-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pgsql #51
pgsql #51
Changes from 23 commits
911835e
2463fcf
244f114
f37203b
be84b01
283a3e8
fdef433
433fe78
8f55319
42a3e2d
2e792de
657ff26
9f6a7a9
98ea9a5
fe23b5b
5a1417e
f388813
362b0a3
ed36ad1
9cb344b
1300373
94533a6
e986d1d
d2a274c
726b2be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
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 NOTHING;`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! I see you added this from your comment from #50. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On second thought there is one reason why you might not want to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated_at time doesn't update on "do nothing". The solution is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, go for it! |
||
r.ID, | ||
strings.ToLower(hash), | ||
) | ||
return err | ||
} |
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicated 'package mysql'?