mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 04:07:31 +00:00
fix(import): add import for app and machine keys (#4536)
* fix(import): add import for app and machine keys * fix(export): add review changes * fix(import): Apply suggestions from code review Co-authored-by: Livio Spring <livio.a@gmail.com> * fix(import): add review changes Co-authored-by: Livio Spring <livio.a@gmail.com>
This commit is contained in:
@@ -84,6 +84,23 @@ type AuthNKey struct {
|
||||
Type domain.AuthNKeyType
|
||||
}
|
||||
|
||||
type AuthNKeysData struct {
|
||||
SearchResponse
|
||||
AuthNKeysData []*AuthNKeyData
|
||||
}
|
||||
|
||||
type AuthNKeyData struct {
|
||||
ID string
|
||||
CreationDate time.Time
|
||||
ResourceOwner string
|
||||
Sequence uint64
|
||||
|
||||
Expiration time.Time
|
||||
Type domain.AuthNKeyType
|
||||
Identifier string
|
||||
PublicKey []byte
|
||||
}
|
||||
|
||||
type AuthNKeySearchQueries struct {
|
||||
SearchRequest
|
||||
Queries []SearchQuery
|
||||
@@ -122,6 +139,30 @@ func (q *Queries) SearchAuthNKeys(ctx context.Context, queries *AuthNKeySearchQu
|
||||
return authNKeys, err
|
||||
}
|
||||
|
||||
func (q *Queries) SearchAuthNKeysData(ctx context.Context, queries *AuthNKeySearchQueries) (authNKeys *AuthNKeysData, err error) {
|
||||
query, scan := prepareAuthNKeysDataQuery()
|
||||
query = queries.toQuery(query)
|
||||
stmt, args, err := query.Where(
|
||||
sq.Eq{
|
||||
AuthNKeyColumnEnabled.identifier(): true,
|
||||
},
|
||||
).ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInvalidArgument(err, "QUERY-SAg3f", "Errors.Query.InvalidRequest")
|
||||
}
|
||||
|
||||
rows, err := q.client.QueryContext(ctx, stmt, args...)
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInternal(err, "QUERY-Dbi53", "Errors.Internal")
|
||||
}
|
||||
authNKeys, err = scan(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
authNKeys.LatestSequence, err = q.latestSequence(ctx, authNKeyTable)
|
||||
return authNKeys, err
|
||||
}
|
||||
|
||||
func (q *Queries) GetAuthNKeyByID(ctx context.Context, shouldTriggerBulk bool, id string, queries ...SearchQuery) (*AuthNKey, error) {
|
||||
if shouldTriggerBulk {
|
||||
projection.AuthNKeyProjection.Trigger(ctx)
|
||||
@@ -270,3 +311,50 @@ func prepareAuthNKeyPublicKeyQuery() (sq.SelectBuilder, func(row *sql.Row) ([]by
|
||||
return publicKey, nil
|
||||
}
|
||||
}
|
||||
|
||||
func prepareAuthNKeysDataQuery() (sq.SelectBuilder, func(rows *sql.Rows) (*AuthNKeysData, error)) {
|
||||
return sq.Select(
|
||||
AuthNKeyColumnID.identifier(),
|
||||
AuthNKeyColumnCreationDate.identifier(),
|
||||
AuthNKeyColumnResourceOwner.identifier(),
|
||||
AuthNKeyColumnSequence.identifier(),
|
||||
AuthNKeyColumnExpiration.identifier(),
|
||||
AuthNKeyColumnType.identifier(),
|
||||
AuthNKeyColumnIdentifier.identifier(),
|
||||
AuthNKeyColumnPublicKey.identifier(),
|
||||
countColumn.identifier(),
|
||||
).From(authNKeyTable.identifier()).PlaceholderFormat(sq.Dollar),
|
||||
func(rows *sql.Rows) (*AuthNKeysData, error) {
|
||||
authNKeys := make([]*AuthNKeyData, 0)
|
||||
var count uint64
|
||||
for rows.Next() {
|
||||
authNKey := new(AuthNKeyData)
|
||||
err := rows.Scan(
|
||||
&authNKey.ID,
|
||||
&authNKey.CreationDate,
|
||||
&authNKey.ResourceOwner,
|
||||
&authNKey.Sequence,
|
||||
&authNKey.Expiration,
|
||||
&authNKey.Type,
|
||||
&authNKey.Identifier,
|
||||
&authNKey.PublicKey,
|
||||
&count,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
authNKeys = append(authNKeys, authNKey)
|
||||
}
|
||||
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, errors.ThrowInternal(err, "QUERY-Dgfn3", "Errors.Query.CloseRows")
|
||||
}
|
||||
|
||||
return &AuthNKeysData{
|
||||
AuthNKeysData: authNKeys,
|
||||
SearchResponse: SearchResponse{
|
||||
Count: count,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
@@ -182,6 +182,189 @@ func Test_AuthNKeyPrepares(t *testing.T) {
|
||||
},
|
||||
object: nil,
|
||||
},
|
||||
{
|
||||
name: "prepareAuthNKeysDataQuery no result",
|
||||
prepare: prepareAuthNKeysDataQuery,
|
||||
want: want{
|
||||
sqlExpectations: mockQueries(
|
||||
regexp.QuoteMeta(`SELECT projections.authn_keys.id,`+
|
||||
` projections.authn_keys.creation_date,`+
|
||||
` projections.authn_keys.resource_owner,`+
|
||||
` projections.authn_keys.sequence,`+
|
||||
` projections.authn_keys.expiration,`+
|
||||
` projections.authn_keys.type,`+
|
||||
` projections.authn_keys.identifier,`+
|
||||
` projections.authn_keys.public_key,`+
|
||||
` COUNT(*) OVER ()`+
|
||||
` FROM projections.authn_keys`),
|
||||
nil,
|
||||
nil,
|
||||
),
|
||||
},
|
||||
object: &AuthNKeysData{AuthNKeysData: []*AuthNKeyData{}},
|
||||
},
|
||||
{
|
||||
name: "prepareAuthNKeysDataQuery one result",
|
||||
prepare: prepareAuthNKeysDataQuery,
|
||||
want: want{
|
||||
sqlExpectations: mockQueries(
|
||||
regexp.QuoteMeta(`SELECT projections.authn_keys.id,`+
|
||||
` projections.authn_keys.creation_date,`+
|
||||
` projections.authn_keys.resource_owner,`+
|
||||
` projections.authn_keys.sequence,`+
|
||||
` projections.authn_keys.expiration,`+
|
||||
` projections.authn_keys.type,`+
|
||||
` projections.authn_keys.identifier,`+
|
||||
` projections.authn_keys.public_key,`+
|
||||
` COUNT(*) OVER ()`+
|
||||
` FROM projections.authn_keys`),
|
||||
[]string{
|
||||
"id",
|
||||
"creation_date",
|
||||
"resource_owner",
|
||||
"sequence",
|
||||
"expiration",
|
||||
"type",
|
||||
"identifier",
|
||||
"public_key",
|
||||
"count",
|
||||
},
|
||||
[][]driver.Value{
|
||||
{
|
||||
"id",
|
||||
testNow,
|
||||
"ro",
|
||||
uint64(20211109),
|
||||
testNow,
|
||||
1,
|
||||
"identifier",
|
||||
[]byte("public"),
|
||||
},
|
||||
},
|
||||
),
|
||||
},
|
||||
object: &AuthNKeysData{
|
||||
SearchResponse: SearchResponse{
|
||||
Count: 1,
|
||||
},
|
||||
AuthNKeysData: []*AuthNKeyData{
|
||||
{
|
||||
ID: "id",
|
||||
CreationDate: testNow,
|
||||
ResourceOwner: "ro",
|
||||
Sequence: 20211109,
|
||||
Expiration: testNow,
|
||||
Type: domain.AuthNKeyTypeJSON,
|
||||
Identifier: "identifier",
|
||||
PublicKey: []byte("public"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "prepareAuthNKeysDataQuery multiple result",
|
||||
prepare: prepareAuthNKeysDataQuery,
|
||||
want: want{
|
||||
sqlExpectations: mockQueries(
|
||||
regexp.QuoteMeta(`SELECT projections.authn_keys.id,`+
|
||||
` projections.authn_keys.creation_date,`+
|
||||
` projections.authn_keys.resource_owner,`+
|
||||
` projections.authn_keys.sequence,`+
|
||||
` projections.authn_keys.expiration,`+
|
||||
` projections.authn_keys.type,`+
|
||||
` projections.authn_keys.identifier,`+
|
||||
` projections.authn_keys.public_key,`+
|
||||
` COUNT(*) OVER ()`+
|
||||
` FROM projections.authn_keys`),
|
||||
[]string{
|
||||
"id",
|
||||
"creation_date",
|
||||
"resource_owner",
|
||||
"sequence",
|
||||
"expiration",
|
||||
"type",
|
||||
"identifier",
|
||||
"public_key",
|
||||
"count",
|
||||
},
|
||||
[][]driver.Value{
|
||||
{
|
||||
"id-1",
|
||||
testNow,
|
||||
"ro",
|
||||
uint64(20211109),
|
||||
testNow,
|
||||
1,
|
||||
"identifier1",
|
||||
[]byte("public1"),
|
||||
},
|
||||
{
|
||||
"id-2",
|
||||
testNow,
|
||||
"ro",
|
||||
uint64(20211109),
|
||||
testNow,
|
||||
1,
|
||||
"identifier2",
|
||||
[]byte("public2"),
|
||||
},
|
||||
},
|
||||
),
|
||||
},
|
||||
object: &AuthNKeysData{
|
||||
SearchResponse: SearchResponse{
|
||||
Count: 2,
|
||||
},
|
||||
AuthNKeysData: []*AuthNKeyData{
|
||||
{
|
||||
ID: "id-1",
|
||||
CreationDate: testNow,
|
||||
ResourceOwner: "ro",
|
||||
Sequence: 20211109,
|
||||
Expiration: testNow,
|
||||
Type: domain.AuthNKeyTypeJSON,
|
||||
Identifier: "identifier1",
|
||||
PublicKey: []byte("public1"),
|
||||
},
|
||||
{
|
||||
ID: "id-2",
|
||||
CreationDate: testNow,
|
||||
ResourceOwner: "ro",
|
||||
Sequence: 20211109,
|
||||
Expiration: testNow,
|
||||
Type: domain.AuthNKeyTypeJSON,
|
||||
Identifier: "identifier2",
|
||||
PublicKey: []byte("public2"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "prepareAuthNKeysDataQuery sql err",
|
||||
prepare: prepareAuthNKeysDataQuery,
|
||||
want: want{
|
||||
sqlExpectations: mockQueryErr(
|
||||
regexp.QuoteMeta(`SELECT projections.authn_keys.id,`+
|
||||
` projections.authn_keys.creation_date,`+
|
||||
` projections.authn_keys.resource_owner,`+
|
||||
` projections.authn_keys.sequence,`+
|
||||
` projections.authn_keys.expiration,`+
|
||||
` projections.authn_keys.type,`+
|
||||
` projections.authn_keys.identifier,`+
|
||||
` projections.authn_keys.public_key,`+
|
||||
` COUNT(*) OVER ()`+
|
||||
` FROM projections.authn_keys`),
|
||||
sql.ErrConnDone,
|
||||
),
|
||||
err: func(err error) (error, bool) {
|
||||
if !errors.Is(err, sql.ErrConnDone) {
|
||||
return fmt.Errorf("err should be sql.ErrConnDone got: %w", err), false
|
||||
}
|
||||
return nil, true
|
||||
},
|
||||
},
|
||||
object: nil,
|
||||
},
|
||||
{
|
||||
name: "prepareAuthNKeyQuery no result",
|
||||
prepare: prepareAuthNKeyQuery,
|
||||
|
Reference in New Issue
Block a user