-
-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add mapping for encoding subject into UUIDs
A new table was added that stores a mapping between an arbitraty string and a UUIDv5 of that string, namespaced by a namespace UUID that the user can set in the namespace configuration.
- Loading branch information
Showing
26 changed files
with
201 additions
and
0 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
Empty file.
10 changes: 10 additions & 0 deletions
10
...stence/sql/migrations/sql/20220110200400000000_create-uuid-mapping-table.cockroach.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,10 @@ | ||
CREATE TABLE keto_uuid_mappings | ||
( | ||
id UUID NOT NULL, | ||
string_representation VARCHAR(64) NOT NULL CHECK (string_representation <> ''), | ||
|
||
PRIMARY KEY (id), | ||
|
||
-- enforce uniqueness | ||
CONSTRAINT chk_keto_uuid_map_uniq UNIQUE (id, string_representation) | ||
); |
Empty file.
10 changes: 10 additions & 0 deletions
10
...ersistence/sql/migrations/sql/20220110200400000000_create-uuid-mapping-table.mysql.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,10 @@ | ||
CREATE TABLE keto_uuid_mappings | ||
( | ||
id VARCHAR(64) NOT NULL, | ||
string_representation VARCHAR(64) NOT NULL CHECK (string_representation <> ''), | ||
|
||
PRIMARY KEY (id), | ||
|
||
-- enforce uniqueness | ||
CONSTRAINT chk_keto_uuid_map_uniq UNIQUE (id, string_representation) | ||
); |
Empty file.
10 changes: 10 additions & 0 deletions
10
...istence/sql/migrations/sql/20220110200400000000_create-uuid-mapping-table.postgres.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,10 @@ | ||
CREATE TABLE keto_uuid_mappings | ||
( | ||
id UUID NOT NULL, | ||
string_representation VARCHAR(64) NOT NULL CHECK (string_representation <> ''), | ||
|
||
PRIMARY KEY (id), | ||
|
||
-- enforce uniqueness | ||
CONSTRAINT chk_keto_uuid_map_uniq UNIQUE (id, string_representation) | ||
); |
Empty file.
10 changes: 10 additions & 0 deletions
10
...sistence/sql/migrations/sql/20220110200400000000_create-uuid-mapping-table.sqlite3.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,10 @@ | ||
CREATE TABLE keto_uuid_mappings | ||
( | ||
id UUID NOT NULL, | ||
string_representation VARCHAR(64) NOT NULL CHECK (string_representation <> ''), | ||
|
||
PRIMARY KEY (id), | ||
|
||
-- enforce uniqueness | ||
CONSTRAINT chk_keto_uuid_map_uniq UNIQUE (id, string_representation) | ||
); |
1 change: 1 addition & 0 deletions
1
...ence/sql/migrations/sql/20220110200400000001_create-uuid-mapping-table.cockroach.down.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 @@ | ||
DROP TABLE keto_uuid_mappings; |
Empty file.
1 change: 1 addition & 0 deletions
1
...sistence/sql/migrations/sql/20220110200400000001_create-uuid-mapping-table.mysql.down.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 @@ | ||
DROP TABLE keto_uuid_mappings; |
Empty file.
1 change: 1 addition & 0 deletions
1
...tence/sql/migrations/sql/20220110200400000001_create-uuid-mapping-table.postgres.down.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 @@ | ||
DROP TABLE keto_uuid_mappings; |
Empty file.
1 change: 1 addition & 0 deletions
1
...stence/sql/migrations/sql/20220110200400000001_create-uuid-mapping-table.sqlite3.down.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 @@ | ||
DROP TABLE keto_uuid_mappings; |
Empty file.
1 change: 1 addition & 0 deletions
1
...al/persistence/sql/migrations/templates/20220110200400_create-uuid-mapping-table.down.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 @@ | ||
DROP TABLE keto_uuid_mappings; |
10 changes: 10 additions & 0 deletions
10
...ersistence/sql/migrations/templates/20220110200400_create-uuid-mapping-table.mysql.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,10 @@ | ||
CREATE TABLE keto_uuid_mappings | ||
( | ||
id VARCHAR(64) NOT NULL, | ||
string_representation VARCHAR(64) NOT NULL CHECK (string_representation <> ''), | ||
|
||
PRIMARY KEY (id), | ||
|
||
-- enforce uniqueness | ||
CONSTRAINT chk_keto_uuid_map_uniq UNIQUE (id, string_representation) | ||
); |
10 changes: 10 additions & 0 deletions
10
...rnal/persistence/sql/migrations/templates/20220110200400_create-uuid-mapping-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,10 @@ | ||
CREATE TABLE keto_uuid_mappings | ||
( | ||
id UUID NOT NULL, | ||
string_representation VARCHAR(64) NOT NULL CHECK (string_representation <> ''), | ||
|
||
PRIMARY KEY (id), | ||
|
||
-- enforce uniqueness | ||
CONSTRAINT chk_keto_uuid_map_uniq UNIQUE (id, string_representation) | ||
); |
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,45 @@ | ||
package sql | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gofrs/uuid" | ||
"github.com/ory/x/sqlcon" | ||
) | ||
|
||
type ( | ||
UUIDMapping struct { | ||
ID uuid.UUID `db:"id"` | ||
StringRepresentation string `db:"string_representation"` | ||
} | ||
UUIDMappings []*UUIDMapping | ||
) | ||
|
||
func (UUIDMappings) TableName() string { | ||
return "keto_uuid_mappings" | ||
} | ||
|
||
func (UUIDMapping) TableName() string { | ||
return "keto_uuid_mappings" | ||
} | ||
|
||
func (p *Persister) AddUUIDMapping(ctx context.Context, id uuid.UUID, representation string) (error) { | ||
m := &UUIDMapping{ | ||
ID: id, | ||
StringRepresentation: representation, | ||
} | ||
p.d.Logger().Trace("adding UUID mapping") | ||
|
||
return sqlcon.HandleError(p.Connection(ctx).Create(m)) | ||
} | ||
|
||
func (p *Persister) LookupUUID(ctx context.Context, id uuid.UUID) (rep string, err error) { | ||
p.d.Logger().Trace("looking up UUID") | ||
|
||
m := &UUIDMapping{} | ||
if err := sqlcon.HandleError(p.Connection(ctx).Find(m, id)); err != nil { | ||
return "", err | ||
} | ||
|
||
return m.StringRepresentation, nil | ||
} |
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 sql_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ory/keto/internal/driver" | ||
"github.com/ory/keto/internal/persistence/sql" | ||
"github.com/ory/keto/internal/x/dbx" | ||
) | ||
|
||
func TestUUIDMapping(t *testing.T) { | ||
for _, dsn := range dbx.GetDSNs(t, false) { | ||
t.Run("dsn="+dsn.Name, func(t *testing.T) { | ||
reg := driver.NewTestRegistry(t, dsn) | ||
c, err := reg.PopConnection() | ||
require.NoError(t, err) | ||
|
||
for _, tc := range []struct { | ||
desc string | ||
mappings interface{} | ||
shouldErr bool | ||
}{{ | ||
desc: "empty should fail on constraint", | ||
mappings: &sql.UUIDMapping{}, | ||
shouldErr: true, | ||
}, { | ||
desc: "single with string rep should succeed", | ||
mappings: &sql.UUIDMapping{StringRepresentation: "foo"}, | ||
shouldErr: false, | ||
}, { | ||
desc: "two with same rep should fail on constraint", | ||
mappings: sql.UUIDMappings{ | ||
&sql.UUIDMapping{StringRepresentation: "bar"}, | ||
&sql.UUIDMapping{StringRepresentation: "bar"}, | ||
}, | ||
shouldErr: true, | ||
}} { | ||
t.Run("case="+tc.desc, func(t *testing.T) { | ||
err = c.Create(tc.mappings) | ||
if tc.shouldErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, 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,17 @@ | ||
package uuidmapping | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gofrs/uuid" | ||
) | ||
|
||
type ( | ||
ManagerProvider interface { | ||
UUIDMappingManager() Manager | ||
} | ||
Manager interface { | ||
AddUUIDMapping(ctx context.Context, id uuid.UUID, representation string) (error) | ||
LookupUUID(ctx context.Context, id uuid.UUID) (rep string, err error) | ||
} | ||
) |