-
Notifications
You must be signed in to change notification settings - Fork 8
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
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
internal/dbmodel/sql/postgres/020_add_ssh_keys_table.up.sql
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,14 @@ | ||
-- Add the ability to store users' SSH public keys | ||
|
||
CREATE TABLE ssh_keys ( | ||
id SERIAL PRIMARY KEY, | ||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), | ||
updated_at TIMESTAMP WITH TIME ZONE, | ||
public_key BYTEA NOT NULL, | ||
key_comment VARCHAR(255), | ||
identity_name TEXT NOT NULL, | ||
FOREIGN KEY (identity_name) REFERENCES identities(name), | ||
CONSTRAINT unique_identity_ssh_key UNIQUE(identity_name, public_key) | ||
); | ||
|
||
CREATE INDEX idx_ssh_keys_user_id ON ssh_keys (identity_name); |
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,23 @@ | ||
// Copyright 2025 Canonical. | ||
|
||
package dbmodel | ||
|
||
import "time" | ||
|
||
// SSHKey holds a user's public SSH key. | ||
type SSHKey struct { | ||
// Note this doesn't use the standard gorm.Model to avoid soft-deletes. | ||
|
||
ID uint `gorm:"primarykey"` | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
|
||
// IdentityName is the unique name (email or client-id) of this entity. | ||
IdentityName string `gorm:"uniqueIndex:unique_identity_ssh_key"` | ||
Identity Identity `gorm:"foreignKey:IdentityName;references:Name"` | ||
|
||
// PublicKey holds the user's public SSH key. | ||
PublicKey []byte `gorm:"uniqueIndex:unique_identity_ssh_key"` | ||
// KeyComment holds a user provided comment. | ||
KeyComment string | ||
} |
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,35 @@ | ||
// Copyright 2025 Canonical. | ||
|
||
package dbmodel_test | ||
|
||
import ( | ||
"testing" | ||
|
||
qt "github.com/frankban/quicktest" | ||
|
||
"github.com/canonical/jimm/v3/internal/dbmodel" | ||
) | ||
|
||
func TestSSHKeyUniqueConstraint(t *testing.T) { | ||
c := qt.New(t) | ||
db := gormDB(c) | ||
|
||
u, err := dbmodel.NewIdentity("bob@canonical.com") | ||
c.Assert(err, qt.IsNil) | ||
|
||
c.Assert(db.Create(u).Error, qt.IsNil) | ||
|
||
key := dbmodel.SSHKey{ | ||
PublicKey: []byte("test"), | ||
Identity: *u, | ||
KeyComment: "foo", | ||
} | ||
c.Assert(db.Create(&key).Error, qt.IsNil) | ||
|
||
newKey := dbmodel.SSHKey{ | ||
PublicKey: []byte("test"), | ||
Identity: *u, | ||
KeyComment: "bar", | ||
} | ||
c.Assert(db.Create(&newKey).Error, qt.ErrorMatches, ".*duplicate key value violates unique constraint \"unique_identity_ssh_key\".*") | ||
} |