-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
202 lines (171 loc) · 5.71 KB
/
client.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Licensed to zntrio under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. zntrio licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package dockerregistry
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/square/go-jose/v3/jwt"
)
// RegistryClient declares Docker Registry contract.
type RegistryClient interface {
Token(ctx context.Context, endpoint, clientID, username, password, service, scope string) (*RegistryToken, error)
}
// -----------------------------------------------------------------------------
// RegistryToken represents a registry token information holder.
type RegistryToken struct {
RegistryURL string
Service string
RequestScopes []string
TokenScopes []string
Token string
AccessToken string
ExpiresAt time.Time
}
// AsMap returns registry token as map.
func (rt *RegistryToken) AsMap() map[string]interface{} {
return map[string]interface{}{
"registry_url": rt.RegistryURL,
"service": rt.Service,
"request_scopes": rt.RequestScopes,
"token_scopes": rt.TokenScopes,
"token": rt.Token,
"access_token": rt.AccessToken,
"expires_at": rt.ExpiresAt.UTC(),
}
}
// -----------------------------------------------------------------------------
type jwtClaims struct {
Audience string `json:"aud"`
Issuer string `json:"iss"`
ExpiresAt uint64 `json:"exp"`
NotBefore uint64 `json:"nbf"`
IssuedAt uint64 `json:"iat"`
Access []jwtAccess `json:"access"`
}
type jwtAccess struct {
Type string `json:"type"`
Name string `json:"name"`
Actions []string `json:"actions"`
}
func (ja *jwtAccess) String() string {
return fmt.Sprintf("%s:%s:%s", ja.Type, ja.Name, strings.Join(ja.Actions, ","))
}
// -----------------------------------------------------------------------------
// tokenResponse represents docker registry token response.
type tokenResponse struct {
Token string `json:"token"`
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
IssuedAt string `json:"issued_at"`
JTI string `json:"jti"`
Subject string `json:"sub"`
}
// -----------------------------------------------------------------------------
type registryClient struct {
httpClient *http.Client
}
// NewRegistryClient returns a default docker registry client implementation.
func NewRegistryClient() RegistryClient {
return ®istryClient{
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
}
}
// Token call external authentication endpoint to rtrieve a session token.
func (rc *registryClient) Token(ctx context.Context, endpoint, clientID, username, password, service, scope string) (*RegistryToken, error) {
// Check arguments
if endpoint == "" {
return nil, errors.New("unable to query registry without endpoint url defined")
}
// Parse endpoint
endpointURL, err := url.Parse(fmt.Sprintf("%s/token", endpoint))
if err != nil {
return nil, fmt.Errorf("endpoint_url is not a valid URL: %v", err)
}
// Prepare context
rctx, rcancel := context.WithTimeout(ctx, 30*time.Second)
defer rcancel()
// Prepare params
params := url.Values{}
if clientID != "" {
params.Add("client_id", clientID)
}
if service != "" {
params.Add("service", service)
}
if scope != "" {
params.Add("scope", scope)
}
endpointURL.RawQuery = params.Encode()
// Prepare request
req, err := http.NewRequestWithContext(rctx, http.MethodGet, endpointURL.String(), nil)
if err != nil {
return nil, fmt.Errorf("unable to prepare docker registry request: %v", err)
}
// Assign basic authentication if credentials provided
if username != "" && password != "" {
req.SetBasicAuth(username, password)
}
// Do the request
resp, err := rc.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("error getting auth token for service='%s' scope='%s': %v", service, scope, err)
}
defer resp.Body.Close()
// Drain body
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error getting auth token for %q: %s", scope, resp.Status)
}
// Extract token response
var data tokenResponse
if err := json.NewDecoder(io.LimitReader(resp.Body, 5<<20)).Decode(&data); err != nil {
return nil, fmt.Errorf("error parsing token for %q: %s", scope, resp.Status)
}
// Decode JWT to extract effective accesses
token, err := jwt.ParseSigned(data.Token)
if err != nil {
return nil, fmt.Errorf("error validating token for %q: %s", scope, resp.Status)
}
// Can't check signature complete certificate chain is not provided
// Extract scope form token
var claims jwtClaims
if err := token.UnsafeClaimsWithoutVerification(&claims); err != nil {
return nil, fmt.Errorf("unable to extract token claims: %v", err)
}
tokenScopes := []string{}
for _, s := range claims.Access {
tokenScopes = append(tokenScopes, s.String())
}
// No error
return &RegistryToken{
RegistryURL: endpoint,
Service: service,
RequestScopes: strings.Split(scope, " "),
TokenScopes: tokenScopes,
Token: data.Token,
AccessToken: data.AccessToken,
ExpiresAt: time.Now().Add(time.Duration(data.ExpiresIn) * time.Second),
}, nil
}