2022-01-12 12:22:04 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rsa"
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2023-02-27 21:36:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/call"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
2023-11-21 12:11:38 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/query/projection"
|
2023-11-21 12:11:38 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/keypair"
|
2022-12-01 08:18:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2022-01-12 12:22:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Key interface {
|
|
|
|
ID() string
|
|
|
|
Algorithm() string
|
|
|
|
Use() domain.KeyUsage
|
|
|
|
Sequence() uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type PrivateKey interface {
|
|
|
|
Key
|
|
|
|
Expiry() time.Time
|
|
|
|
Key() *crypto.CryptoValue
|
|
|
|
}
|
|
|
|
|
|
|
|
type PublicKey interface {
|
|
|
|
Key
|
|
|
|
Expiry() time.Time
|
|
|
|
Key() interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PrivateKeys struct {
|
|
|
|
SearchResponse
|
|
|
|
Keys []PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
type PublicKeys struct {
|
|
|
|
SearchResponse
|
|
|
|
Keys []PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
type key struct {
|
|
|
|
id string
|
|
|
|
creationDate time.Time
|
|
|
|
changeDate time.Time
|
|
|
|
sequence uint64
|
|
|
|
resourceOwner string
|
|
|
|
algorithm string
|
|
|
|
use domain.KeyUsage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *key) ID() string {
|
|
|
|
return k.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *key) Algorithm() string {
|
|
|
|
return k.algorithm
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *key) Use() domain.KeyUsage {
|
|
|
|
return k.use
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *key) Sequence() uint64 {
|
|
|
|
return k.sequence
|
|
|
|
}
|
|
|
|
|
|
|
|
type privateKey struct {
|
|
|
|
key
|
|
|
|
expiry time.Time
|
|
|
|
privateKey *crypto.CryptoValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *privateKey) Expiry() time.Time {
|
|
|
|
return k.expiry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *privateKey) Key() *crypto.CryptoValue {
|
|
|
|
return k.privateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
type rsaPublicKey struct {
|
|
|
|
key
|
|
|
|
expiry time.Time
|
|
|
|
publicKey *rsa.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rsaPublicKey) Expiry() time.Time {
|
|
|
|
return r.expiry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rsaPublicKey) Key() interface{} {
|
|
|
|
return r.publicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
keyTable = table{
|
2022-10-27 06:08:36 +00:00
|
|
|
name: projection.KeyProjectionTable,
|
|
|
|
instanceIDCol: projection.KeyColumnInstanceID,
|
2022-01-12 12:22:04 +00:00
|
|
|
}
|
|
|
|
KeyColID = Column{
|
|
|
|
name: projection.KeyColumnID,
|
|
|
|
table: keyTable,
|
|
|
|
}
|
|
|
|
KeyColCreationDate = Column{
|
|
|
|
name: projection.KeyColumnCreationDate,
|
|
|
|
table: keyTable,
|
|
|
|
}
|
|
|
|
KeyColChangeDate = Column{
|
|
|
|
name: projection.KeyColumnChangeDate,
|
|
|
|
table: keyTable,
|
|
|
|
}
|
|
|
|
KeyColResourceOwner = Column{
|
|
|
|
name: projection.KeyColumnResourceOwner,
|
|
|
|
table: keyTable,
|
|
|
|
}
|
2022-03-23 08:02:39 +00:00
|
|
|
KeyColInstanceID = Column{
|
|
|
|
name: projection.KeyColumnInstanceID,
|
|
|
|
table: keyTable,
|
|
|
|
}
|
2022-01-12 12:22:04 +00:00
|
|
|
KeyColSequence = Column{
|
|
|
|
name: projection.KeyColumnSequence,
|
|
|
|
table: keyTable,
|
|
|
|
}
|
|
|
|
KeyColAlgorithm = Column{
|
|
|
|
name: projection.KeyColumnAlgorithm,
|
|
|
|
table: keyTable,
|
|
|
|
}
|
|
|
|
KeyColUse = Column{
|
|
|
|
name: projection.KeyColumnUse,
|
|
|
|
table: keyTable,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
keyPrivateTable = table{
|
2022-10-27 06:08:36 +00:00
|
|
|
name: projection.KeyPrivateTable,
|
|
|
|
instanceIDCol: projection.KeyPrivateColumnInstanceID,
|
2022-01-12 12:22:04 +00:00
|
|
|
}
|
|
|
|
KeyPrivateColID = Column{
|
|
|
|
name: projection.KeyPrivateColumnID,
|
|
|
|
table: keyPrivateTable,
|
|
|
|
}
|
|
|
|
KeyPrivateColExpiry = Column{
|
|
|
|
name: projection.KeyPrivateColumnExpiry,
|
|
|
|
table: keyPrivateTable,
|
|
|
|
}
|
|
|
|
KeyPrivateColKey = Column{
|
|
|
|
name: projection.KeyPrivateColumnKey,
|
|
|
|
table: keyPrivateTable,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
keyPublicTable = table{
|
2022-10-27 06:08:36 +00:00
|
|
|
name: projection.KeyPublicTable,
|
|
|
|
instanceIDCol: projection.KeyPrivateColumnInstanceID,
|
2022-01-12 12:22:04 +00:00
|
|
|
}
|
|
|
|
KeyPublicColID = Column{
|
|
|
|
name: projection.KeyPublicColumnID,
|
|
|
|
table: keyPublicTable,
|
|
|
|
}
|
|
|
|
KeyPublicColExpiry = Column{
|
|
|
|
name: projection.KeyPublicColumnExpiry,
|
|
|
|
table: keyPublicTable,
|
|
|
|
}
|
|
|
|
KeyPublicColKey = Column{
|
|
|
|
name: projection.KeyPublicColumnKey,
|
|
|
|
table: keyPublicTable,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-08-22 12:49:02 +00:00
|
|
|
func (q *Queries) ActivePublicKeys(ctx context.Context, t time.Time) (keys *PublicKeys, err error) {
|
2022-12-01 08:18:53 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
query, scan := preparePublicKeysQuery(ctx, q.client)
|
2022-01-12 12:22:04 +00:00
|
|
|
if t.IsZero() {
|
|
|
|
t = time.Now()
|
|
|
|
}
|
|
|
|
stmt, args, err := query.Where(
|
2022-03-23 08:02:39 +00:00
|
|
|
sq.And{
|
2022-11-30 16:01:17 +00:00
|
|
|
sq.Eq{KeyColInstanceID.identifier(): authz.GetInstance(ctx).InstanceID()},
|
|
|
|
sq.Gt{KeyPublicColExpiry.identifier(): t},
|
2022-01-12 12:22:04 +00:00
|
|
|
}).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-SDFfg", "Errors.Query.SQLStatement")
|
|
|
|
}
|
|
|
|
|
2023-08-22 12:49:02 +00:00
|
|
|
err = q.client.QueryContext(ctx, func(rows *sql.Rows) error {
|
|
|
|
keys, err = scan(rows)
|
|
|
|
return err
|
|
|
|
}, stmt, args...)
|
2022-01-12 12:22:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-Sghn4", "Errors.Internal")
|
|
|
|
}
|
2023-08-22 12:49:02 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
keys.State, err = q.latestState(ctx, keyTable)
|
2022-09-12 16:18:08 +00:00
|
|
|
if !errors.IsNotFound(err) {
|
|
|
|
return keys, err
|
|
|
|
}
|
|
|
|
return keys, nil
|
2022-01-12 12:22:04 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 12:49:02 +00:00
|
|
|
func (q *Queries) ActivePrivateSigningKey(ctx context.Context, t time.Time) (keys *PrivateKeys, err error) {
|
2022-12-01 08:18:53 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
stmt, scan := preparePrivateKeysQuery(ctx, q.client)
|
2022-01-12 12:22:04 +00:00
|
|
|
if t.IsZero() {
|
|
|
|
t = time.Now()
|
|
|
|
}
|
|
|
|
query, args, err := stmt.Where(
|
|
|
|
sq.And{
|
|
|
|
sq.Eq{
|
2022-03-23 08:02:39 +00:00
|
|
|
KeyColUse.identifier(): domain.KeyUsageSigning,
|
2022-03-29 09:53:19 +00:00
|
|
|
KeyColInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
|
2022-01-12 12:22:04 +00:00
|
|
|
},
|
2022-11-30 16:01:17 +00:00
|
|
|
sq.Gt{KeyPrivateColExpiry.identifier(): t},
|
2022-01-12 12:22:04 +00:00
|
|
|
}).OrderBy(KeyPrivateColExpiry.identifier()).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-SDff2", "Errors.Query.SQLStatement")
|
|
|
|
}
|
|
|
|
|
2023-08-22 12:49:02 +00:00
|
|
|
err = q.client.QueryContext(ctx, func(rows *sql.Rows) error {
|
|
|
|
keys, err = scan(rows)
|
|
|
|
return err
|
|
|
|
}, query, args...)
|
2022-01-12 12:22:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-WRFG4", "Errors.Internal")
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
keys.State, err = q.latestState(ctx, keyTable)
|
2022-01-28 08:24:34 +00:00
|
|
|
if !errors.IsNotFound(err) {
|
|
|
|
return keys, err
|
|
|
|
}
|
|
|
|
return keys, nil
|
2022-01-12 12:22:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
func preparePublicKeysQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Rows) (*PublicKeys, error)) {
|
2022-01-12 12:22:04 +00:00
|
|
|
return sq.Select(
|
|
|
|
KeyColID.identifier(),
|
|
|
|
KeyColCreationDate.identifier(),
|
|
|
|
KeyColChangeDate.identifier(),
|
|
|
|
KeyColSequence.identifier(),
|
|
|
|
KeyColResourceOwner.identifier(),
|
|
|
|
KeyColAlgorithm.identifier(),
|
|
|
|
KeyColUse.identifier(),
|
|
|
|
KeyPublicColExpiry.identifier(),
|
|
|
|
KeyPublicColKey.identifier(),
|
|
|
|
countColumn.identifier(),
|
|
|
|
).From(keyTable.identifier()).
|
2023-02-27 21:36:43 +00:00
|
|
|
LeftJoin(join(KeyPublicColID, KeyColID) + db.Timetravel(call.Took(ctx))).
|
2022-01-12 12:22:04 +00:00
|
|
|
PlaceholderFormat(sq.Dollar),
|
|
|
|
func(rows *sql.Rows) (*PublicKeys, error) {
|
|
|
|
keys := make([]PublicKey, 0)
|
|
|
|
var count uint64
|
|
|
|
for rows.Next() {
|
|
|
|
k := new(rsaPublicKey)
|
|
|
|
var keyValue []byte
|
|
|
|
err := rows.Scan(
|
|
|
|
&k.id,
|
|
|
|
&k.creationDate,
|
|
|
|
&k.changeDate,
|
|
|
|
&k.sequence,
|
|
|
|
&k.resourceOwner,
|
|
|
|
&k.algorithm,
|
|
|
|
&k.use,
|
|
|
|
&k.expiry,
|
|
|
|
&keyValue,
|
|
|
|
&count,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
k.publicKey, err = crypto.BytesToPublicKey(keyValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-rKd6k", "Errors.Query.CloseRows")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PublicKeys{
|
|
|
|
Keys: keys,
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: count,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
func preparePrivateKeysQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Rows) (*PrivateKeys, error)) {
|
2022-01-12 12:22:04 +00:00
|
|
|
return sq.Select(
|
|
|
|
KeyColID.identifier(),
|
|
|
|
KeyColCreationDate.identifier(),
|
|
|
|
KeyColChangeDate.identifier(),
|
|
|
|
KeyColSequence.identifier(),
|
|
|
|
KeyColResourceOwner.identifier(),
|
|
|
|
KeyColAlgorithm.identifier(),
|
|
|
|
KeyColUse.identifier(),
|
|
|
|
KeyPrivateColExpiry.identifier(),
|
|
|
|
KeyPrivateColKey.identifier(),
|
|
|
|
countColumn.identifier(),
|
|
|
|
).From(keyTable.identifier()).
|
2023-02-27 21:36:43 +00:00
|
|
|
LeftJoin(join(KeyPrivateColID, KeyColID) + db.Timetravel(call.Took(ctx))).
|
2022-01-12 12:22:04 +00:00
|
|
|
PlaceholderFormat(sq.Dollar),
|
|
|
|
func(rows *sql.Rows) (*PrivateKeys, error) {
|
|
|
|
keys := make([]PrivateKey, 0)
|
|
|
|
var count uint64
|
|
|
|
for rows.Next() {
|
|
|
|
k := new(privateKey)
|
|
|
|
err := rows.Scan(
|
|
|
|
&k.id,
|
|
|
|
&k.creationDate,
|
|
|
|
&k.changeDate,
|
|
|
|
&k.sequence,
|
|
|
|
&k.resourceOwner,
|
|
|
|
&k.algorithm,
|
|
|
|
&k.use,
|
|
|
|
&k.expiry,
|
|
|
|
&k.privateKey,
|
|
|
|
&count,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-rKd6k", "Errors.Query.CloseRows")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PrivateKeys{
|
|
|
|
Keys: keys,
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: count,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
2023-11-21 12:11:38 +00:00
|
|
|
|
|
|
|
type PublicKeyReadModel struct {
|
|
|
|
eventstore.ReadModel
|
|
|
|
|
|
|
|
Algorithm string
|
|
|
|
Key *crypto.CryptoValue
|
|
|
|
Expiry time.Time
|
|
|
|
Usage domain.KeyUsage
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPublicKeyReadModel(keyID, resourceOwner string) *PublicKeyReadModel {
|
|
|
|
return &PublicKeyReadModel{
|
|
|
|
ReadModel: eventstore.ReadModel{
|
|
|
|
AggregateID: keyID,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *PublicKeyReadModel) AppendEvents(events ...eventstore.Event) {
|
|
|
|
wm.ReadModel.AppendEvents(events...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *PublicKeyReadModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *keypair.AddedEvent:
|
|
|
|
wm.Algorithm = e.Algorithm
|
|
|
|
wm.Key = e.PublicKey.Key
|
|
|
|
wm.Expiry = e.PublicKey.Expiry
|
|
|
|
wm.Usage = e.Usage
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.ReadModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *PublicKeyReadModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AwaitOpenTransactions().
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(keypair.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(keypair.AddedEventType).
|
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetActivePublicKeyByID(ctx context.Context, keyID string, current time.Time) (_ PublicKey, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
model := NewPublicKeyReadModel(keyID, authz.GetInstance(ctx).InstanceID())
|
|
|
|
if err := q.eventstore.FilterToQueryReducer(ctx, model); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if model.Algorithm == "" || model.Key == nil {
|
|
|
|
return nil, errors.ThrowNotFound(err, "QUERY-Ahf7x", "Errors.Key.NotFound")
|
|
|
|
}
|
|
|
|
if model.Expiry.Before(current) {
|
|
|
|
return nil, errors.ThrowInvalidArgument(err, "QUERY-ciF4k", "Errors.Key.ExpireBeforeNow")
|
|
|
|
}
|
|
|
|
keyValue, err := crypto.Decrypt(model.Key, q.keyEncryptionAlgorithm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-Ie4oh", "Errors.Internal")
|
|
|
|
}
|
|
|
|
publicKey, err := crypto.BytesToPublicKey(keyValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-Kai2Z", "Errors.Internal")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rsaPublicKey{
|
|
|
|
key: key{
|
|
|
|
id: model.AggregateID,
|
|
|
|
creationDate: model.CreationDate,
|
|
|
|
changeDate: model.ChangeDate,
|
|
|
|
sequence: model.ProcessedSequence,
|
|
|
|
resourceOwner: model.ResourceOwner,
|
|
|
|
algorithm: model.Algorithm,
|
|
|
|
use: model.Usage,
|
|
|
|
},
|
|
|
|
expiry: model.Expiry,
|
|
|
|
publicKey: publicKey,
|
|
|
|
}, nil
|
|
|
|
}
|