Skip to content

Commit

Permalink
Add godocs
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Bulatov <[email protected]>
  • Loading branch information
Oleg Bulatov committed Mar 13, 2017
1 parent faec495 commit e27dc0c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/dockerregistry/server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func (ac *AccessController) Authorized(ctx context.Context, accessRecords ...reg
// Conditionally add auth errors we want to handle later to the context
if !possibleCrossMountErrors.Empty() {
context.GetLogger(ctx).Debugf("Origin auth: deferring errors: %#v", possibleCrossMountErrors)
ctx = WithDeferredErrors(ctx, possibleCrossMountErrors)
ctx = withDeferredErrors(ctx, possibleCrossMountErrors)
}
// Always add a marker to the context so we know auth was run
ctx = WithAuthPerformed(ctx)
Expand Down
2 changes: 1 addition & 1 deletion pkg/dockerregistry/server/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func TestAccessController(t *testing.T) {
t.Errorf("%s: expected AuthPerformed to be true", k)
continue
}
deferredErrors, hasDeferred := DeferredErrorsFrom(authCtx)
deferredErrors, hasDeferred := deferredErrorsFrom(authCtx)
if len(test.expectedRepoErr) > 0 {
if !hasDeferred || deferredErrors[test.expectedRepoErr] == nil {
t.Errorf("%s: expected deferred error for repo %s, got none", k, test.expectedRepoErr)
Expand Down
36 changes: 29 additions & 7 deletions pkg/dockerregistry/server/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,71 +12,93 @@ const (
// repositoryKey serves to store/retrieve repository object to/from context.
repositoryKey contextKey = "repository"

// remoteBlobAccessCheckEnabledKey allows blobDescriptorService to stat remote blobs which is useful only
// in case of manifest verification.
// remoteBlobAccessCheckEnabledKey is the key for the flag in Contexts
// to allow blobDescriptorService to stat remote blobs.
remoteBlobAccessCheckEnabledKey contextKey = "remoteBlobAccessCheckEnabled"

// registryClientKey is a key to store RegistryClient to context.
// registryClientKey is the key for RegistryClient values in Contexts.
registryClientKey contextKey = "registryClient"

// userClientKey is the key for a origin's client with the current user's
// credentials in Contexts.
userClientKey contextKey = "userClient"

// authPerformedKey is the key to indicate that authentication was
// performed in Contexts.
authPerformedKey contextKey = "authPerformed"

// deferredErrorsKey is the key for deferred errors in Contexts.
deferredErrorsKey contextKey = "deferredErrors"
)

// WithRepository returns a new Context that carries value repo.
func WithRepository(parent context.Context, repo *repository) context.Context {
return context.WithValue(parent, repositoryKey, repo)
}

// RepositoryFrom returns the repository value stored in ctx, if any.
func RepositoryFrom(ctx context.Context) (repo *repository, found bool) {
repo, found = ctx.Value(repositoryKey).(*repository)
return
}

// WithRemoteBlobAccessCheckEnabled returns a new Context that allows
// blobDescriptorService to stat remote blobs. It is useful only in case
// of manifest verification.
func WithRemoteBlobAccessCheckEnabled(parent context.Context, enable bool) context.Context {
return context.WithValue(parent, remoteBlobAccessCheckEnabledKey, enable)
}

// RemoteBlobAccessCheckEnabledFrom reports whether ctx allows
// blobDescriptorService to stat remote blobs.
func RemoteBlobAccessCheckEnabledFrom(ctx context.Context) bool {
enabled, _ := ctx.Value(remoteBlobAccessCheckEnabledKey).(bool)
return enabled
}

// WithRegistryClient creates a new context with provided registry client.
// WithRegistryClient returns a new Context with provided registry client.
func WithRegistryClient(ctx context.Context, client RegistryClient) context.Context {
return context.WithValue(ctx, registryClientKey, client)
}

// RegistryClientFrom returns the registry client stored in ctx, if present. It will panic otherwise.
// RegistryClientFrom returns the registry client stored in ctx, if present.
// It will panic otherwise.
func RegistryClientFrom(ctx context.Context) RegistryClient {
return ctx.Value(registryClientKey).(RegistryClient)
}

// WithUserClient returns a new Context with the origin's client.
// This client should have the current user's credentials
func WithUserClient(parent context.Context, userClient client.Interface) context.Context {
return context.WithValue(parent, userClientKey, userClient)
}

// UserClientFrom returns the origin's client stored in ctx, if any.
func UserClientFrom(ctx context.Context) (client.Interface, bool) {
userClient, ok := ctx.Value(userClientKey).(client.Interface)
return userClient, ok
}

// WithAuthPerformed returns a new Context with indication that authentication
// was performed.
func WithAuthPerformed(parent context.Context) context.Context {
return context.WithValue(parent, authPerformedKey, true)
}

// AuthPerformed reports whether ctx has indication that authentication was
// performed.
func AuthPerformed(ctx context.Context) bool {
authPerformed, ok := ctx.Value(authPerformedKey).(bool)
return ok && authPerformed
}

func WithDeferredErrors(parent context.Context, errs deferredErrors) context.Context {
// withDeferredErrors returns a new Context that carries deferred errors.
func withDeferredErrors(parent context.Context, errs deferredErrors) context.Context {
return context.WithValue(parent, deferredErrorsKey, errs)
}

func DeferredErrorsFrom(ctx context.Context) (deferredErrors, bool) {
// deferredErrorsFrom returns the deferred errors stored in ctx, if any.
func deferredErrorsFrom(ctx context.Context) (deferredErrors, bool) {
errs, ok := ctx.Value(deferredErrorsKey).(deferredErrors)
return errs, ok
}
2 changes: 1 addition & 1 deletion pkg/dockerregistry/server/repositorymiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func checkPendingErrors(ctx context.Context, logger context.Logger, namespace, n
return fmt.Errorf("openshift.auth.completed missing from context")
}

deferredErrors, haveDeferredErrors := DeferredErrorsFrom(ctx)
deferredErrors, haveDeferredErrors := deferredErrorsFrom(ctx)
if !haveDeferredErrors {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/dockerregistry/server/repositorymiddleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func TestRepositoryBlobStat(t *testing.T) {
ctx = WithAuthPerformed(ctx)
}
if tc.deferredErrors != nil {
ctx = WithDeferredErrors(ctx, tc.deferredErrors)
ctx = withDeferredErrors(ctx, tc.deferredErrors)
}

client := &testclient.Fake{}
Expand Down

0 comments on commit e27dc0c

Please sign in to comment.