Skip to content

Commit 7c071af

Browse files
committed
feat(api): resolve external owners during create
1 parent f30f37a commit 7c071af

8 files changed

Lines changed: 1076 additions & 37 deletions

api/create_request_normalization.go

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
type createRequestError struct {
1616
status int
1717
message string
18+
data any
1819
err error
1920
}
2021

@@ -34,30 +35,43 @@ func newCreateRequestError(status int, err error) error {
3435
}
3536
}
3637

38+
func newCreateRequestErrorWithData(status int, message string, data any, err error) error {
39+
return &createRequestError{
40+
status: status,
41+
message: message,
42+
data: data,
43+
err: err,
44+
}
45+
}
46+
3747
func writeCreateRequestError(c echo.Context, err error) error {
3848
var requestErr *createRequestError
3949
if errors.As(err, &requestErr) {
50+
if requestErr.data != nil {
51+
return writeJSendFailData(c, requestErr.status, requestErr.data)
52+
}
4053
return writeError(c, requestErr.status, requestErr.message)
4154
}
4255
return writeError(c, http.StatusInternalServerError, err.Error())
4356
}
4457

4558
type normalizedCreateRequest struct {
46-
body createRequest
47-
fingerprintRequest createRequest
48-
namespace string
49-
owner spritzv1.SpritzOwner
50-
userConfigKeys map[string]json.RawMessage
51-
userConfigPayload userConfigPayload
52-
normalizedUserConfig json.RawMessage
53-
requestedImage bool
54-
requestedRepo bool
55-
requestedNamespace bool
56-
nameProvided bool
57-
requestedNamePrefix string
59+
body createRequest
60+
fingerprintRequest createRequest
61+
namespace string
62+
owner spritzv1.SpritzOwner
63+
resolvedExternalOwner *externalOwnerResolution
64+
userConfigKeys map[string]json.RawMessage
65+
userConfigPayload userConfigPayload
66+
normalizedUserConfig json.RawMessage
67+
requestedImage bool
68+
requestedRepo bool
69+
requestedNamespace bool
70+
nameProvided bool
71+
requestedNamePrefix string
5872
}
5973

60-
func (s *server) normalizeCreateRequest(_ context.Context, principal principal, body createRequest) (*normalizedCreateRequest, error) {
74+
func (s *server) normalizeCreateRequest(ctx context.Context, principal principal, body createRequest) (*normalizedCreateRequest, error) {
6175
body.Name = strings.TrimSpace(body.Name)
6276
body.NamePrefix = strings.TrimSpace(body.NamePrefix)
6377
applyTopLevelCreateFields(&body)
@@ -73,12 +87,22 @@ func (s *server) normalizeCreateRequest(_ context.Context, principal principal,
7387
}
7488
requestedNamespace := s.namespaceOverrideRequested(body.Namespace, namespace)
7589

76-
owner, err := normalizeCreateOwner(&body, principal, s.auth.enabled())
90+
owner, resolvedExternalOwner, err := s.resolveCreateOwner(ctx, &body, principal)
7791
if err != nil {
92+
var resolutionErr externalOwnerResolutionError
93+
if errors.As(err, &resolutionErr) {
94+
return nil, newCreateRequestErrorWithData(resolutionErr.status, resolutionErr.message, resolutionErr.responseData(), err)
95+
}
96+
if errors.Is(err, errForbidden) {
97+
return nil, newCreateRequestError(http.StatusForbidden, err)
98+
}
7899
return nil, newCreateRequestError(http.StatusBadRequest, err)
79100
}
80101
body.Spec.Owner = owner
81102
fingerprintRequest := body
103+
if resolvedExternalOwner != nil {
104+
fingerprintRequest.Spec.Owner = spritzv1.SpritzOwner{}
105+
}
82106

83107
requestedImage := strings.TrimSpace(body.Spec.Image) != ""
84108
requestedRepo := body.Spec.Repo != nil || len(body.Spec.Repos) > 0
@@ -121,18 +145,19 @@ func (s *server) normalizeCreateRequest(_ context.Context, principal principal,
121145
}
122146

123147
return &normalizedCreateRequest{
124-
body: body,
125-
fingerprintRequest: fingerprintRequest,
126-
namespace: namespace,
127-
owner: owner,
128-
userConfigKeys: userConfigKeys,
129-
userConfigPayload: userConfigPayload,
130-
normalizedUserConfig: normalizedUserConfig,
131-
requestedImage: requestedImage,
132-
requestedRepo: requestedRepo,
133-
requestedNamespace: requestedNamespace,
134-
nameProvided: body.Name != "",
135-
requestedNamePrefix: strings.TrimSpace(fingerprintRequest.NamePrefix),
148+
body: body,
149+
fingerprintRequest: fingerprintRequest,
150+
namespace: namespace,
151+
owner: owner,
152+
resolvedExternalOwner: resolvedExternalOwner,
153+
userConfigKeys: userConfigKeys,
154+
userConfigPayload: userConfigPayload,
155+
normalizedUserConfig: normalizedUserConfig,
156+
requestedImage: requestedImage,
157+
requestedRepo: requestedRepo,
158+
requestedNamespace: requestedNamespace,
159+
nameProvided: body.Name != "",
160+
requestedNamePrefix: strings.TrimSpace(fingerprintRequest.NamePrefix),
136161
}, nil
137162
}
138163

0 commit comments

Comments
 (0)