-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,216 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package pgsql | ||
|
||
import ( | ||
"github.com/micromdm/nanomdm/mdm" | ||
) | ||
|
||
func (s *PgSQLStorage) StoreBootstrapToken(r *mdm.Request, msg *mdm.SetBootstrapToken) error { | ||
_, err := s.db.ExecContext( | ||
r.Context, | ||
`UPDATE devices SET bootstrap_token_b64 = $1, bootstrap_token_at = CURRENT_TIMESTAMP WHERE id = $2;`, | ||
nullEmptyString(msg.BootstrapToken.BootstrapToken.String()), | ||
r.ID, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
return s.updateLastSeen(r) | ||
} | ||
|
||
func (s *PgSQLStorage) RetrieveBootstrapToken(r *mdm.Request, _ *mdm.GetBootstrapToken) (*mdm.BootstrapToken, error) { | ||
var tokenB64 string | ||
err := s.db.QueryRowContext( | ||
r.Context, | ||
`SELECT bootstrap_token_b64 FROM devices WHERE id = $1;`, | ||
r.ID, | ||
).Scan(&tokenB64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
bsToken := new(mdm.BootstrapToken) | ||
err = bsToken.SetTokenString(tokenB64) | ||
if err == nil { | ||
err = s.updateLastSeen(r) | ||
} | ||
return bsToken, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package pgsql | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/micromdm/nanomdm/mdm" | ||
) | ||
|
||
// Executes SQL statements that return a single COUNT(*) of rows. | ||
func (s *PgSQLStorage) queryRowContextRowExists(ctx context.Context, query string, args ...interface{}) (bool, error) { | ||
var ct int | ||
err := s.db.QueryRowContext(ctx, query, args...).Scan(&ct) | ||
return ct > 0, err | ||
} | ||
|
||
func (s *PgSQLStorage) EnrollmentHasCertHash(r *mdm.Request, _ string) (bool, error) { | ||
return s.queryRowContextRowExists( | ||
r.Context, | ||
`SELECT COUNT(*) FROM cert_auth_associations WHERE id = $1;`, | ||
r.ID, | ||
) | ||
} | ||
|
||
func (s *PgSQLStorage) HasCertHash(r *mdm.Request, hash string) (bool, error) { | ||
return s.queryRowContextRowExists( | ||
r.Context, | ||
`SELECT COUNT(*) FROM cert_auth_associations WHERE sha256 = $1;`, | ||
strings.ToLower(hash), | ||
) | ||
} | ||
|
||
func (s *PgSQLStorage) IsCertHashAssociated(r *mdm.Request, hash string) (bool, error) { | ||
return s.queryRowContextRowExists( | ||
r.Context, | ||
`SELECT COUNT(*) FROM cert_auth_associations WHERE id = $1 AND sha256 = $2;`, | ||
r.ID, strings.ToLower(hash), | ||
) | ||
} | ||
|
||
// AssociateCertHash "DO NOTHING" on duplicated keys | ||
func (s *PgSQLStorage) AssociateCertHash(r *mdm.Request, hash string) error { | ||
_, err := s.db.ExecContext( | ||
r.Context, ` | ||
INSERT INTO cert_auth_associations (id, sha256) | ||
VALUES ($1, $2) | ||
ON CONFLICT ON CONSTRAINT cert_auth_associations_pkey DO UPDATE SET updated_at=now();`, | ||
r.ID, | ||
strings.ToLower(hash), | ||
) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package pgsql | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/micromdm/nanomdm/mdm" | ||
) | ||
|
||
func (s *PgSQLStorage) RetrieveMigrationCheckins(ctx context.Context, c chan<- interface{}) error { | ||
// TODO: if a TokenUpdate does not include the latest UnlockToken | ||
// then we should synthesize a TokenUpdate to transfer it over. | ||
deviceRows, err := s.db.QueryContext( | ||
ctx, | ||
`SELECT authenticate, token_update FROM devices;`, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
defer deviceRows.Close() | ||
for deviceRows.Next() { | ||
var authBytes, tokenBytes []byte | ||
if err := deviceRows.Scan(&authBytes, &tokenBytes); err != nil { | ||
return err | ||
} | ||
for _, msgBytes := range [][]byte{authBytes, tokenBytes} { | ||
msg, err := mdm.DecodeCheckin(msgBytes) | ||
if err != nil { | ||
c <- err | ||
} else { | ||
c <- msg | ||
} | ||
} | ||
} | ||
if err = deviceRows.Err(); err != nil { | ||
return err | ||
} | ||
userRows, err := s.db.QueryContext( | ||
ctx, | ||
`SELECT token_update FROM users;`, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
defer userRows.Close() | ||
for userRows.Next() { | ||
var msgBytes []byte | ||
if err := userRows.Scan(&msgBytes); err != nil { | ||
return err | ||
} | ||
msg, err := mdm.DecodeCheckin(msgBytes) | ||
if err != nil { | ||
c <- err | ||
} else { | ||
c <- msg | ||
} | ||
} | ||
if err = userRows.Err(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.