Skip to content

Commit

Permalink
refactor (shuffle around) storage interfaces into storage package
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Jul 17, 2023
1 parent 43e7ab8 commit 60d9089
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
5 changes: 2 additions & 3 deletions cmd/kmfddm/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ type allStorage interface {
ddm.StatusStorage
api.EnrollmentAPIStorage
api.StatusAPIStorage
storage.Toucher
storage.DeclarationAPIStorage
storage.EnrollmentIDRetriever
storage.DeclarationRetriever
storage.TokensDeclarationItemsRetriever
storage.EnrollmentDeclarationStorage
}

var hasher func() hash.Hash = func() hash.Hash { return xxhash.New() }
Expand Down
3 changes: 1 addition & 2 deletions http/api/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ type DeclarationAPIStorage interface {
RetrieveDeclaration(ctx context.Context, declarationID string) (*ddm.Declaration, error)
DeleteDeclaration(ctx context.Context, declarationID string) (bool, error)
RetrieveDeclarationSets(ctx context.Context, declarationID string) (setNames []string, err error)
StoreDeclaration(ctx context.Context, d *ddm.Declaration) (bool, error)
RetrieveDeclarations(ctx context.Context) ([]string, error)
}

// PutDeclarationHandler stores a new or overwrites an existing declaration.
func PutDeclarationHandler(store DeclarationAPIStorage, notifier Notifier, logger log.Logger) http.HandlerFunc {
func PutDeclarationHandler(store storage.DeclarationStorer, notifier Notifier, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := ctxlog.Logger(r.Context(), logger)
bodyBytes, err := io.ReadAll(r.Body)
Expand Down
26 changes: 25 additions & 1 deletion storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@ package storage

import (
"context"

"github.com/jessepeterson/kmfddm/ddm"
)

type Toucher interface {
// TouchDeclaration forces a change only to a declaration's ServerToken.
// TouchDeclaration forces a change to a declaration's ServerToken only.
TouchDeclaration(ctx context.Context, declarationID string) error
}

type DeclarationStorer interface {
// StoreDeclaration stores a declaration.
//
// Note that a storage backend may tried to creation relations
// based on the the ddm.IdentifierRefs field.
StoreDeclaration(ctx context.Context, d *ddm.Declaration) (bool, error)
}

// DeclarationAPIStorage are storage interfaces relating to declaration APIs.
type DeclarationAPIStorage interface {
Toucher
DeclarationStorer
}

type EnrollmentIDRetriever interface {
// RetrieveEnrollmentIDs retrieves MDM enrollment IDs from storage.
// In the case of sets and declarations the transitive associations
Expand All @@ -22,10 +38,12 @@ type EnrollmentIDRetriever interface {
}

type TokensJSONRetriever interface {
// RetrieveTokensJSON returns the token JSON for an enrollment ID.
RetrieveTokensJSON(ctx context.Context, enrollmentID string) ([]byte, error)
}

type TokensDeclarationItemsRetriever interface {
// RetrieveDeclarationItemsJSON returns the declaration items JSON for an enrollment ID.
RetrieveDeclarationItemsJSON(ctx context.Context, enrollmentID string) ([]byte, error)
TokensJSONRetriever
}
Expand All @@ -34,3 +52,9 @@ type DeclarationRetriever interface {
// RetrieveEnrollmentDeclarationJSON fetches the JSON for a declaration for an enrollment ID.
RetrieveEnrollmentDeclarationJSON(ctx context.Context, declarationID, declarationType, enrollmentID string) ([]byte, error)
}

// EnrollmentDeclarationStorage is the storage required to support declarations in the DDM protocol.
type EnrollmentDeclarationStorage interface {
TokensDeclarationItemsRetriever
DeclarationRetriever
}
2 changes: 1 addition & 1 deletion storage/test/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type allTestStorage interface {
setAndDeclStorage
api.EnrollmentAPIStorage
storage.TokensDeclarationItemsRetriever
storage.Toucher
storage.EnrollmentIDRetriever
storage.DeclarationAPIStorage
}

func TestBasic(t *testing.T, storage allTestStorage, ctx context.Context) {
Expand Down
2 changes: 1 addition & 1 deletion storage/test/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type declStorage interface {
api.DeclarationAPIStorage
storage.Toucher
storage.DeclarationAPIStorage
}

func testStoreDeclaration(t *testing.T, storage declStorage, ctx context.Context, decl *ddm.Declaration) {
Expand Down
2 changes: 1 addition & 1 deletion storage/test/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type statusStorage interface {
httpddm.StatusStorage
api.StatusAPIStorage
api.DeclarationAPIStorage
storage.DeclarationStorer
api.EnrollmentAPIStorage
api.SetAPIStorage
}
Expand Down

0 comments on commit 60d9089

Please sign in to comment.