-
Notifications
You must be signed in to change notification settings - Fork 1
/
artifacts.go
109 lines (92 loc) · 2.73 KB
/
artifacts.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
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"strings"
)
func getArtifactSHA(clientId, tenant, server, user, password, apiVersion, project, image string) (string, error) {
artifact := strings.Split(image, ":")
if len(artifact) < 2 {
artifact = append(artifact, "latest")
}
// artifact[0] = url.QueryEscape(artifact[0])
artifact[0] = strings.ReplaceAll(artifact[0], "/", "%252F")
idtoken, err := getOidcBearer(clientId, tenant, server, user, password)
if err != nil {
log.Fatal("There were an error getting idtoken")
}
//log.Println(idtoken)
bearer := fmt.Sprintf("Bearer %v", idtoken)
url := fmt.Sprintf("%v/api/%vprojects/%v/repositories/%v/artifacts/%v",
server,
apiVersion,
project,
artifact[0],
artifact[1],
)
var artifactResponse ArtifactResponse
res, body := client(
ClientPrt{
Url: url,
Method: "GET",
ContentType: "application/json",
User: user,
Password: password,
Bearer: bearer,
},
)
if res.StatusCode < 399 && res.StatusCode > 100 {
err := json.Unmarshal([]byte(body), &artifactResponse)
if err != nil {
return "", errors.New("There was an error getting the artifact")
}
} else {
log.Fatal("Error getting artifacts")
return "", errors.New("There was an error getting the artifact")
}
return artifactResponse.Digest, nil
}
func checkArtifactSHA(clientId, tenant, server, user, password, apiVersion, project, image, sha string) bool {
artifact, e := getArtifactSHA(clientId, tenant, server, user, password, apiVersion, project, image)
if e != nil {
return false
}
return strings.EqualFold(artifact, sha)
}
func getOidcBearer(clientId, tenant, server, user, password string) (string, error) {
url := fmt.Sprintf("https://login.microsoftonline.com/%v/oauth2/v2.0/token",
tenant,
)
var oidcTokenRequest OidcTokenRequest = OidcTokenRequest{}
oidcTokenRequest.Client_id = clientId
oidcTokenRequest.Password = password
oidcTokenRequest.Username = user
oidcTokenRequest.Response_type = "id_token"
oidcTokenRequest.Grant_type = "password"
oidcTokenRequest.Scope = "openid"
res, body := client(
ClientPrt{
Url: url,
Method: "POST",
ContentType: "application/x-www-form-urlencoded",
Body: oidcTokenRequest,
},
)
var oidcTokenResponse OidcTokenResponse
if res.StatusCode < 399 && res.StatusCode > 100 {
//log.Println(res)
//log.Println(body)
err := json.Unmarshal([]byte(body), &oidcTokenResponse)
if err != nil {
log.Fatal(err)
return "", errors.New("There was an error getting the oidc token")
}
} else {
log.Fatal("Error calling oidc endpoint")
return "", errors.New("There was an error getting oidc token")
}
//log.Println(oidcTokenResponse)
return oidcTokenResponse.IdToken, nil
}