Skip to content

Commit

Permalink
return enrollment ID string rather than mdm request struct
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Aug 27, 2023
1 parent ee33b3c commit 4913cb8
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 20 deletions.
6 changes: 3 additions & 3 deletions http/mdm/mdm_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func CertWithEnrollmentIDMiddleware(next http.Handler, hasher HashFn, store stor
return
}
}
mr, err := store.EnrollmentFromHash(r.Context(), hasher(cert))
id, err := store.EnrollmentFromHash(r.Context(), hasher(cert))
if err != nil {
ctxlog.Logger(r.Context(), logger).Info(
"msg", "retreiving enrollment from hash",
Expand All @@ -177,7 +177,7 @@ func CertWithEnrollmentIDMiddleware(next http.Handler, hasher HashFn, store stor
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if mr == nil || mr.ID == "" {
if id == "" {
if enforce {
ctxlog.Logger(r.Context(), logger).Info(
"err", "missing enrollment id",
Expand All @@ -192,7 +192,7 @@ func CertWithEnrollmentIDMiddleware(next http.Handler, hasher HashFn, store stor
return
}
}
ctx := context.WithValue(r.Context(), contextEnrollmentID, mr.ID)
ctx := context.WithValue(r.Context(), contextEnrollmentID, id)
next.ServeHTTP(w, r.WithContext(ctx))
}
}
4 changes: 2 additions & 2 deletions storage/allmulti/certauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func (ms *MultiAllStorage) AssociateCertHash(r *mdm.Request, hash string) error
return err
}

func (ms *MultiAllStorage) EnrollmentFromHash(ctx context.Context, hash string) (*mdm.Request, error) {
func (ms *MultiAllStorage) EnrollmentFromHash(ctx context.Context, hash string) (string, error) {
val, err := ms.execStores(ctx, func(s storage.AllStorage) (interface{}, error) {
return s.EnrollmentFromHash(ctx, hash)
})
return val.(*mdm.Request), err
return val.(string), err
}
10 changes: 5 additions & 5 deletions storage/file/certauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ func (s *FileStorage) AssociateCertHash(r *mdm.Request, hash string) error {
return e.writeFile(CertAuthFilename, []byte(hash))
}

func (s *FileStorage) EnrollmentFromHash(_ context.Context, hash string) (*mdm.Request, error) {
func (s *FileStorage) EnrollmentFromHash(_ context.Context, hash string) (string, error) {
f, err := os.Open(path.Join(s.path, CertAuthAssociationsFilename))
if err != nil {
return nil, err
return "", err
}
defer f.Close()
scanner := bufio.NewScanner(f)
Expand All @@ -82,10 +82,10 @@ func (s *FileStorage) EnrollmentFromHash(_ context.Context, hash string) (*mdm.R
if strings.Contains(text, hash) {
split := strings.Split(text, ",")
if len(split) < 2 {
return nil, errors.New("hash and enrollment id not present on line")
return "", errors.New("hash and enrollment id not present on line")
}
return &mdm.Request{EnrollID: &mdm.EnrollID{ID: split[0]}}, nil
return split[0], nil
}
}
return nil, nil
return "", nil
}
6 changes: 3 additions & 3 deletions storage/mysql/certauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ UPDATE sha256 = new.sha256;`,
return err
}

func (s *MySQLStorage) EnrollmentFromHash(ctx context.Context, hash string) (*mdm.Request, error) {
func (s *MySQLStorage) EnrollmentFromHash(ctx context.Context, hash string) (string, error) {
var id string
err := s.db.QueryRowContext(
ctx,
`SELECT id FROM cert_auth_associations WHERE sha256 = ? LIMIT 1;`,
hash,
).Scan(&id)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
return "", nil
}
return &mdm.Request{EnrollID: &mdm.EnrollID{ID: id}}, err
return id, err
}
3 changes: 3 additions & 0 deletions storage/mysql/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ SELECT command_uuid FROM commands WHERE command_uuid = ? FOR UPDATE;
`,
uuid,
)
if err != nil {
return err
}
// delete command result (i.e. NotNows) and this queued command
_, err = tx.ExecContext(
ctx, `
Expand Down
6 changes: 3 additions & 3 deletions storage/pgsql/certauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ ON CONFLICT ON CONSTRAINT cert_auth_associations_pkey DO UPDATE SET updated_at=n
return err
}

func (s *PgSQLStorage) EnrollmentFromHash(ctx context.Context, hash string) (*mdm.Request, error) {
func (s *PgSQLStorage) EnrollmentFromHash(ctx context.Context, hash string) (string, error) {
var id string
err := s.db.QueryRowContext(
ctx,
`SELECT id FROM cert_auth_associations WHERE sha256 = $1 LIMIT 1;`,
hash,
).Scan(&id)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
return "", nil
}
return &mdm.Request{EnrollID: &mdm.EnrollID{ID: id}}, err
return id, err
}
7 changes: 3 additions & 4 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ type CertAuthStore interface {
}

type CertAuthRetriever interface {
// EnrollmentFromHash retrieves an MDM request from a cert hash.
// Implementations should return a nil pointer if no result is found.
// The ID member ought to be populated when non-nil.
EnrollmentFromHash(ctx context.Context, hash string) (*mdm.Request, error)
// EnrollmentFromHash retrieves an enrollment ID from a cert hash.
// Implementations should return an empty string if no result is found.
EnrollmentFromHash(ctx context.Context, hash string) (string, error)
}

// StoreMigrator retrieves MDM check-ins
Expand Down

0 comments on commit 4913cb8

Please sign in to comment.