-
Notifications
You must be signed in to change notification settings - Fork 8
/
registry.go
163 lines (130 loc) · 4.41 KB
/
registry.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package nscloud
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/artifacts/registry"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/tasks"
)
var DefaultKeychain oci.Keychain = defaultKeychain{}
const loginEndpoint = "login.namespace.so/token"
type nscloudRegistry struct {
clusterID string
registry *api.ImageRegistry
}
const registryAddr = "registry-fgfo23t6gn9jd834s36g.prod-metal.namespacelabs.nscloud.dev"
func RegisterRegistry() {
registry.Register("nscloud", func(ctx context.Context, ck cfg.Configuration) (registry.Manager, error) {
conf, ok := clusterConfigType.CheckGet(ck)
if !ok || conf.ClusterId == "" {
return nil, fnerrors.InternalError("missing registry configuration")
}
return nscloudRegistry{clusterID: conf.ClusterId}, nil
})
oci.RegisterDomainKeychain(registryAddr, DefaultKeychain, oci.Keychain_UseAlways)
}
func (r nscloudRegistry) Access() oci.RegistryAccess {
return oci.RegistryAccess{
InsecureRegistry: false,
Keychain: defaultKeychain{},
}
}
func (r nscloudRegistry) AllocateName(repository string) compute.Computable[oci.RepositoryWithParent] {
return compute.Map(tasks.Action("nscloud.allocate-repository").Arg("repository", repository),
compute.Inputs().Str("repository", repository).Str("clusterID", r.clusterID),
compute.Output{},
func(ctx context.Context, _ compute.Resolved) (oci.RepositoryWithParent, error) {
registry, err := r.fetchRegistry(ctx)
if err != nil {
return oci.RepositoryWithParent{}, err
}
url := registry.EndpointAddress
if url == "" {
return oci.RepositoryWithParent{}, fnerrors.InternalError("%s: cluster is missing registry", r.clusterID)
}
if strings.HasSuffix(url, "/") {
url += repository
} else {
url += "/" + repository
}
return oci.RepositoryWithParent{
Parent: r,
RepositoryWithAccess: oci.RepositoryWithAccess{
Repository: url,
RegistryAccess: r.Access(),
},
}, nil
})
}
func (r nscloudRegistry) fetchRegistry(ctx context.Context) (*api.ImageRegistry, error) {
if r.registry != nil {
return r.registry, nil
}
resp, err := api.GetCluster(ctx, api.Endpoint, r.clusterID)
if err != nil {
return nil, err
}
return resp.Registry, nil
}
type defaultKeychain struct{}
func (dk defaultKeychain) Resolve(ctx context.Context, r authn.Resource) (authn.Authenticator, error) {
if !strings.HasSuffix(r.RegistryStr(), ".nscluster.cloud") {
return authn.Anonymous, nil
}
user, err := fnapi.LoadUser()
if err != nil {
return nil, err
}
ref, err := name.ParseReference(r.String())
if err != nil {
return nil, err
}
values := url.Values{}
values.Add("scope", fmt.Sprintf("repository:%s:push,pull", ref.Context().RepositoryStr()))
values.Add("service", "Authentication")
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://%s?%s", loginEndpoint, values.Encode()), nil)
if err != nil {
return nil, err
}
tok, err := user.GenerateToken(ctx)
if err != nil {
return nil, err
}
req.Header.Add("X-Namespace-Token", tok)
req.Header.Add("Authorization", "Bearer "+tok)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fnerrors.InvocationError("nscloud", "%s: unexpected status when fetching an access token: %d", r, resp.StatusCode)
}
tokenData, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fnerrors.InvocationError("nscloud", "%s: unexpected error when fetching an access token: %w", r, err)
}
var t Token
if err := json.Unmarshal(tokenData, &t); err != nil {
return nil, fnerrors.InvocationError("nscloud", "%s: unexpected error when unmarshalling an access token: %w", r, err)
}
return &authn.Bearer{Token: t.Token}, nil
}
type Token struct {
Token string `json:"token"`
}