-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker-credential-ocir.go
157 lines (137 loc) · 3.25 KB
/
docker-credential-ocir.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
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/oracle/oci-go-sdk/v63/common"
"github.com/oracle/oci-go-sdk/v63/common/auth"
)
type DockerToken struct {
Token string `json:"token"`
Scope string `json:"scope,omitempty"`
Expires int64 `json:"expires_in,omitempty"`
}
type TokenResponse struct {
ServerURL string `json:"ServerURL"`
Username string `json:"Username"`
Secret string `json:"Secret"`
}
func envOr(key, def string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return def
}
var (
usr, _ = user.Current()
dir = usr.HomeDir
ociConf = filepath.Join(dir, ".oci", "config")
)
func main() {
var cp common.ConfigurationProvider
var err error
if filepath.Base(os.Args[0]) == "docker-credential-ocir" {
cp, err = auth.InstancePrincipalConfigurationProvider()
} else {
cp, err = common.ConfigurationProviderFromFileWithProfile(
envOr("OCI_CLI_CONFIG_FILE", ociConf),
envOr("OCI_CLI_PROFILE", "DEFAULT"),
envOr("OCI_CLI_PASSPHRASE", ""), // in an env var?!
)
}
if err != nil {
panic(err)
}
switch strings.ToLower(os.Args[1]) {
case "store":
return
case "erase":
return
case "get":
var rawUrl string
if _, err := fmt.Scanln(&rawUrl); err != nil {
panic(err)
}
rawUrl = strings.TrimSpace(strings.ToLower(rawUrl))
if rawUrl[:8] == "https://" {
rawUrl = rawUrl[8:]
}
// if rawUrl[len(rawUrl)-8:] != ".ocir.io" {
// fmt.Println("Only *.ocir.io registry URLs are supported")
// os.Exit(1)
// }
// Try to get an example repo manifest
var realm string
if resp, err := http.Get(fmt.Sprintf("https://%s/v2/", rawUrl)); err != nil {
panic(err)
} else {
resp.Body.Close()
authHeader := resp.Header.Get("www-authenticate")
// Bearer realm="https://phx.ocir.io/20180419/docker/token",service="phx.ocir.io",scope=""
parts := strings.Split(authHeader, " ")
if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" {
panic("Unexpected www-authenticate header")
}
values := map[string]string{}
for _, part := range strings.Split(parts[1], ",") {
kv := strings.SplitN(part, "=", 2)
if len(kv) == 2 {
values[strings.ToLower(kv[0])] = strings.Trim(kv[1], "\"")
}
}
realm = values["realm"]
}
if realm == "" {
panic("no realm returned in initial registry handshake")
}
registryUrl, err := url.Parse(realm)
if err != nil {
panic(err)
}
cl, err := common.NewClientWithConfig(cp)
if err != nil {
panic(err)
}
cl.Host = registryUrl.Host
resp, err := cl.Call(context.Background(), &http.Request{
Method: "GET",
URL: registryUrl,
Header: map[string][]string{
"Accept": {"application/json"},
},
})
if err != nil {
panic(err)
}
if resp.Body != nil {
defer resp.Body.Close()
} else {
panic("no body in return")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var token DockerToken
if err := json.Unmarshal(body, &token); err != nil {
panic(err)
}
tr := TokenResponse{
ServerURL: rawUrl,
Username: "BEARER_TOKEN",
Secret: token.Token,
}
out, err := json.Marshal(tr)
if err != nil {
panic(err)
}
fmt.Println(string(out))
}
}