/
apitoken.go
214 lines (181 loc) · 6.67 KB
/
apitoken.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
203
204
205
206
207
208
209
210
211
212
213
214
//
// Copyright 2024 The Chainloop Authors.
//
// Licensed 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 biz
import (
"context"
"errors"
"fmt"
"time"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/authz"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/conf"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/jwt"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/jwt/apitoken"
"github.com/chainloop-dev/chainloop/internal/servicelogger"
"github.com/go-kratos/kratos/v2/log"
"github.com/google/uuid"
)
// API Token is used for unattended access to the control plane API.
type APIToken struct {
ID uuid.UUID
Name string
Description string
// This is the JWT value returned only during creation
JWT string
// Tokens are scoped to organizations
OrganizationID uuid.UUID
CreatedAt *time.Time
// When the token expires
ExpiresAt *time.Time
// When the token was manually revoked
RevokedAt *time.Time
}
type APITokenRepo interface {
Create(ctx context.Context, name string, description *string, expiresAt *time.Time, organizationID uuid.UUID) (*APIToken, error)
// List all the tokens optionally filtering it by organization and including revoked tokens
List(ctx context.Context, orgID *uuid.UUID, includeRevoked bool) ([]*APIToken, error)
Revoke(ctx context.Context, orgID, ID uuid.UUID) error
FindByID(ctx context.Context, ID uuid.UUID) (*APIToken, error)
}
type APITokenUseCase struct {
apiTokenRepo APITokenRepo
logger *log.Helper
jwtBuilder *apitoken.Builder
enforcer *authz.Enforcer
DefaultAuthzPolicies []*authz.Policy
}
type APITokenSyncerUseCase struct {
base *APITokenUseCase
}
func NewAPITokenUseCase(apiTokenRepo APITokenRepo, conf *conf.Auth, authzE *authz.Enforcer, logger log.Logger) (*APITokenUseCase, error) {
uc := &APITokenUseCase{
apiTokenRepo: apiTokenRepo,
logger: servicelogger.ScopedHelper(logger, "biz/APITokenUseCase"),
enforcer: authzE,
DefaultAuthzPolicies: []*authz.Policy{
// Add permissions to workflow contract management
authz.PolicyWorkflowContractList, authz.PolicyWorkflowContractRead, authz.PolicyWorkflowContractUpdate,
// to download artifacts and list referrers
authz.PolicyArtifactDownload, authz.PolicyReferrerRead,
authz.PolicyOrganizationRead,
},
}
// Create the JWT builder for the API token
b, err := apitoken.NewBuilder(
apitoken.WithIssuer(jwt.DefaultIssuer),
apitoken.WithKeySecret(conf.GeneratedJwsHmacSecret),
)
if err != nil {
return nil, fmt.Errorf("creating jwt builder: %w", err)
}
uc.jwtBuilder = b
return uc, nil
}
// expires in is a string that can be parsed by time.ParseDuration
func (uc *APITokenUseCase) Create(ctx context.Context, name string, description *string, expiresIn *time.Duration, orgID string) (*APIToken, error) {
orgUUID, err := uuid.Parse(orgID)
if err != nil {
return nil, NewErrInvalidUUID(err)
}
if name == "" {
return nil, NewErrValidationStr("name is required")
}
// validate format of the name and the project
if err := ValidateIsDNS1123(name); err != nil {
return nil, NewErrValidation(err)
}
// If expiration is provided we store it
// we also validate that it's at least 24 hours and valid string format
var expiresAt *time.Time
if expiresIn != nil {
expiresAt = new(time.Time)
*expiresAt = time.Now().Add(*expiresIn)
}
// NOTE: the expiration time is stored just for reference, it's also encoded in the JWT
// We store it since Chainloop will not have access to the JWT to check the expiration once created
token, err := uc.apiTokenRepo.Create(ctx, name, description, expiresAt, orgUUID)
if err != nil {
if errors.Is(err, ErrAlreadyExists) {
return nil, NewErrValidationStr("name already taken")
}
return nil, fmt.Errorf("storing token: %w", err)
}
// generate the JWT
token.JWT, err = uc.jwtBuilder.GenerateJWT(token.ID.String(), expiresAt)
if err != nil {
return nil, fmt.Errorf("generating jwt: %w", err)
}
// Add default policies to the enforcer
if err := uc.enforcer.AddPolicies(&authz.SubjectAPIToken{ID: token.ID.String()}, uc.DefaultAuthzPolicies...); err != nil {
return nil, fmt.Errorf("adding default policies: %w", err)
}
return token, nil
}
func (uc *APITokenUseCase) List(ctx context.Context, orgID string, includeRevoked bool) ([]*APIToken, error) {
orgUUID, err := uuid.Parse(orgID)
if err != nil {
return nil, NewErrInvalidUUID(err)
}
return uc.apiTokenRepo.List(ctx, &orgUUID, includeRevoked)
}
func (uc *APITokenUseCase) Revoke(ctx context.Context, orgID, id string) error {
orgUUID, err := uuid.Parse(orgID)
if err != nil {
return NewErrInvalidUUID(err)
}
uuid, err := uuid.Parse(id)
if err != nil {
return NewErrInvalidUUID(err)
}
// clean policies
if err := uc.enforcer.ClearPolicies(&authz.SubjectAPIToken{ID: id}); err != nil {
return fmt.Errorf("removing policies: %w", err)
}
return uc.apiTokenRepo.Revoke(ctx, orgUUID, uuid)
}
func (uc *APITokenUseCase) FindByID(ctx context.Context, id string) (*APIToken, error) {
uuid, err := uuid.Parse(id)
if err != nil {
return nil, NewErrInvalidUUID(err)
}
t, err := uc.apiTokenRepo.FindByID(ctx, uuid)
if err != nil {
return nil, fmt.Errorf("finding token: %w", err)
} else if t == nil {
return nil, NewErrNotFound("token")
}
return t, nil
}
func NewAPITokenSyncerUseCase(tokenUC *APITokenUseCase) *APITokenSyncerUseCase {
return &APITokenSyncerUseCase{
base: tokenUC,
}
}
// Make sure all the API tokens contain the default policies
// NOTE: We'll remove this method once we have a proper policies management system where the user can add/remove policies
func (suc *APITokenSyncerUseCase) SyncPolicies() error {
suc.base.logger.Info("syncing policies for all the API tokens")
// List all the non-revoked tokens from all the orgs
tokens, err := suc.base.apiTokenRepo.List(context.Background(), nil, false)
if err != nil {
return fmt.Errorf("listing tokens: %w", err)
}
for _, t := range tokens {
// Add default policies to the enforcer
if err := suc.base.enforcer.AddPolicies(&authz.SubjectAPIToken{ID: t.ID.String()}, suc.base.DefaultAuthzPolicies...); err != nil {
return fmt.Errorf("adding default policies: %w", err)
}
}
return nil
}