-
Notifications
You must be signed in to change notification settings - Fork 81
/
token.go
177 lines (157 loc) · 4.92 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package app
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/golang-jwt/jwt/v4"
gt "github.com/google/go-github/v61/github"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/github"
)
type Install struct {
request *http.Request
run *params.Run
repo *v1alpha1.Repository
ghClient *github.Provider
namespace string
repoList []string
}
func NewInstallation(req *http.Request, run *params.Run, repo *v1alpha1.Repository, gh *github.Provider, namespace string) *Install {
if req == nil {
req = &http.Request{}
}
return &Install{
request: req,
run: run,
repo: repo,
ghClient: gh,
namespace: namespace,
}
}
func (ip *Install) GetAndUpdateInstallationID(ctx context.Context) (string, string, int64, error) {
var (
enterpriseHost, token string
installationID int64
)
jwtToken, err := ip.GenerateJWT(ctx)
if err != nil {
return "", "", 0, err
}
installationURL := *ip.ghClient.APIURL + keys.InstallationURL
enterpriseHost = ip.request.Header.Get("X-GitHub-Enterprise-Host")
if enterpriseHost != "" {
// NOTE: Hopefully this works even when the ghe URL is on another host than the api URL
installationURL = "https://" + enterpriseHost + "/api/v3" + keys.InstallationURL
}
res, err := GetReponse(ctx, http.MethodGet, installationURL, jwtToken, ip.run)
if err != nil {
return "", "", 0, err
}
if res.StatusCode >= 300 {
return "", "", 0, fmt.Errorf("Non-OK HTTP status while getting installation URL: %s : %d", installationURL, res.StatusCode)
}
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
return "", "", 0, err
}
installationData := []gt.Installation{}
if err = json.Unmarshal(data, &installationData); err != nil {
return "", "", 0, err
}
/* each installationID can have list of repository
ref: https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps#authenticating-as-an-installation ,
https://docs.github.com/en/rest/apps/installations?apiVersion=2022-11-28#list-repositories-accessible-to-the-app-installation */
for i := range installationData {
if installationData[i].ID == nil {
return "", "", 0, fmt.Errorf("installation ID is nil")
}
if *installationData[i].ID != 0 {
token, err = ip.ghClient.GetAppToken(ctx, ip.run.Clients.Kube, enterpriseHost, *installationData[i].ID, ip.namespace)
if err != nil {
return "", "", 0, err
}
}
exist, err := ip.listRepos(ctx)
if err != nil {
return "", "", 0, err
}
if exist {
installationID = *installationData[i].ID
break
}
}
return enterpriseHost, token, installationID, nil
}
func (ip *Install) listRepos(ctx context.Context) (bool, error) {
if ip.repoList == nil {
var err error
ip.repoList, err = github.ListRepos(ctx, ip.ghClient)
if err != nil {
return false, err
}
}
for i := range ip.repoList {
// If URL matches with repo spec url then we can break for loop
if ip.repoList[i] == ip.repo.Spec.URL {
return true, nil
}
}
return false, nil
}
type JWTClaim struct {
Issuer int64 `json:"iss"`
jwt.RegisteredClaims
}
func (ip *Install) GenerateJWT(ctx context.Context) (string, error) {
// TODO: move this out of here
gh := github.New()
gh.Run = ip.run
applicationID, privateKey, err := gh.GetAppIDAndPrivateKey(ctx, ip.namespace, ip.run.Clients.Kube)
if err != nil {
return "", err
}
// The expirationTime claim identifies the expiration time on or after which the JWT MUST NOT be accepted for processing.
// Value cannot be longer duration.
// See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4
expirationTime := time.Now().Add(5 * time.Minute)
claims := &JWTClaim{
Issuer: applicationID,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(expirationTime),
IssuedAt: jwt.NewNumericDate(time.Now()),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
parsedPK, err := jwt.ParseRSAPrivateKeyFromPEM(privateKey)
if err != nil {
return "", fmt.Errorf("failed to parse private key: %w", err)
}
tokenString, err := token.SignedString(parsedPK)
if err != nil {
return "", fmt.Errorf("failed to sign private key: %w", err)
}
return tokenString, nil
}
func GetReponse(ctx context.Context, method, urlData, jwtToken string, run *params.Run) (*http.Response, error) {
rawurl, err := url.Parse(urlData)
if err != nil {
return nil, err
}
newreq, err := http.NewRequestWithContext(ctx, method, rawurl.String(), nil)
if err != nil {
return nil, err
}
newreq.Header = map[string][]string{
"Accept": {"application/vnd.github+json"},
"Authorization": {fmt.Sprintf("Bearer %s", jwtToken)},
}
res, err := run.Clients.HTTP.Do(newreq)
return res, err
}