Files
zitadel/internal/key/repository/view/model/key.go
Livio Amstutz f60d200d5a fix: improve key rotation (#1107)
* key rotation

* fix: rotate signing key

* cleanup

* introspect

* testingapplication key

* date

* client keys

* fix client keys

* fix client keys

* access tokens only for users

* AuthMethodPrivateKeyJWT

* client keys

* set introspection info correctly

* managae apis

* update oidc pkg

* cleanup

* merge msater

* set current sequence in migration

* set current sequence in migration

* set current sequence in migration

* ensure authn keys uptodate

* improve key rotation

* fix: return api config in ApplicationView

* fix mocks for tests

* fix(mock): corrected unit tests for updated mock package

Co-authored-by: Stefan Benz <stefan@caos.ch>
2021-02-23 15:07:42 +01:00

89 lines
2.6 KiB
Go

package model
import (
"database/sql"
"encoding/json"
"time"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/crypto"
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/models"
"github.com/caos/zitadel/internal/key/model"
es_model "github.com/caos/zitadel/internal/key/repository/eventsourcing/model"
)
const (
KeyKeyID = "id"
KeyPrivate = "private"
KeyUsage = "usage"
KeyAlgorithm = "algorithm"
KeyExpiry = "expiry"
)
type KeyView struct {
ID string `json:"-" gorm:"column:id;primary_key"`
Private sql.NullBool `json:"-" gorm:"column:private;primary_key"`
Expiry time.Time `json:"-" gorm:"column:expiry"`
Algorithm string `json:"-" gorm:"column:algorithm"`
Usage int32 `json:"-" gorm:"column:usage"`
Key *crypto.CryptoValue `json:"-" gorm:"column:key"`
Sequence uint64 `json:"-" gorm:"column:sequence"`
}
func KeysFromPairEvent(event *models.Event) (*KeyView, *KeyView, error) {
pair := new(es_model.KeyPair)
if err := json.Unmarshal(event.Data, pair); err != nil {
logging.Log("MODEL-s3Ga1").WithError(err).Error("could not unmarshal event data")
return nil, nil, caos_errs.ThrowInternal(nil, "MODEL-G3haa", "could not unmarshal data")
}
privateKey := &KeyView{
ID: event.AggregateID,
Private: sql.NullBool{Bool: true, Valid: true},
Expiry: pair.PrivateKey.Expiry,
Algorithm: pair.Algorithm,
Usage: pair.Usage,
Key: pair.PrivateKey.Key,
Sequence: event.Sequence,
}
publicKey := &KeyView{
ID: event.AggregateID,
Private: sql.NullBool{Bool: false, Valid: true},
Expiry: pair.PublicKey.Expiry,
Algorithm: pair.Algorithm,
Usage: pair.Usage,
Key: pair.PublicKey.Key,
Sequence: event.Sequence,
}
return privateKey, publicKey, nil
}
func KeyViewsToModel(keys []*KeyView) []*model.KeyView {
converted := make([]*model.KeyView, len(keys))
for i, key := range keys {
converted[i] = KeyViewToModel(key)
}
return converted
}
func KeyViewToModel(key *KeyView) *model.KeyView {
return &model.KeyView{
ID: key.ID,
Private: key.Private.Bool,
Expiry: key.Expiry,
Algorithm: key.Algorithm,
Usage: model.KeyUsage(key.Usage),
Key: key.Key,
Sequence: key.Sequence,
}
}
func (k *KeyView) setData(event *models.Event) error {
if err := json.Unmarshal(event.Data, k); err != nil {
logging.Log("MODEL-4ag41").WithError(err).Error("could not unmarshal event data")
return caos_errs.ThrowInternal(nil, "MODEL-GFQ31", "could not unmarshal data")
}
return nil
}