mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 21:37:32 +00:00
feat(v3alpha): web key resource (#8262)
# Which Problems Are Solved Implement a new API service that allows management of OIDC signing web keys. This allows users to manage rotation of the instance level keys. which are currently managed based on expiry. The API accepts the generation of the following key types and parameters: - RSA keys with 2048, 3072 or 4096 bit in size and: - Signing with SHA-256 (RS256) - Signing with SHA-384 (RS384) - Signing with SHA-512 (RS512) - ECDSA keys with - P256 curve - P384 curve - P512 curve - ED25519 keys # How the Problems Are Solved Keys are serialized for storage using the JSON web key format from the `jose` library. This is the format that will be used by OIDC for signing, verification and publication. Each instance can have a number of key pairs. All existing public keys are meant to be used for token verification and publication the keys endpoint. Keys can be activated and the active private key is meant to sign new tokens. There is always exactly 1 active signing key: 1. When the first key for an instance is generated, it is automatically activated. 2. Activation of the next key automatically deactivates the previously active key. 3. Keys cannot be manually deactivated from the API 4. Active keys cannot be deleted # Additional Changes - Query methods that later will be used by the OIDC package are already implemented. Preparation for #8031 - Fix indentation in french translation for instance event - Move user_schema translations to consistent positions in all translation files # Additional Context - Closes #8030 - Part of #7809 --------- Co-authored-by: Elio Bischof <elio@zitadel.com>
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-jose/go-jose/v4"
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
@@ -76,6 +77,7 @@ type Commands struct {
|
||||
defaultSecretGenerators *SecretGenerators
|
||||
|
||||
samlCertificateAndKeyGenerator func(id string) ([]byte, []byte, error)
|
||||
webKeyGenerator func(keyID string, alg crypto.EncryptionAlgorithm, genConfig crypto.WebKeyConfig) (encryptedPrivate *crypto.CryptoValue, public *jose.JSONWebKey, err error)
|
||||
|
||||
GrpcMethodExisting func(method string) bool
|
||||
GrpcServiceExisting func(method string) bool
|
||||
@@ -157,6 +159,7 @@ func StartCommands(
|
||||
defaultRefreshTokenIdleLifetime: defaultRefreshTokenIdleLifetime,
|
||||
defaultSecretGenerators: defaultSecretGenerators,
|
||||
samlCertificateAndKeyGenerator: samlCertificateAndKeyGenerator(defaults.KeyConfig.CertificateSize, defaults.KeyConfig.CertificateLifetime),
|
||||
webKeyGenerator: crypto.GenerateEncryptedWebKey,
|
||||
// always true for now until we can check with an eventlist
|
||||
EventExisting: func(event string) bool { return true },
|
||||
// always true for now until we can check with an eventlist
|
||||
|
@@ -34,12 +34,20 @@ const (
|
||||
)
|
||||
|
||||
type InstanceSetup struct {
|
||||
zitadel ZitadelConfig
|
||||
InstanceName string
|
||||
CustomDomain string
|
||||
DefaultLanguage language.Tag
|
||||
Org InstanceOrgSetup
|
||||
SecretGenerators *SecretGenerators
|
||||
zitadel ZitadelConfig
|
||||
InstanceName string
|
||||
CustomDomain string
|
||||
DefaultLanguage language.Tag
|
||||
Org InstanceOrgSetup
|
||||
SecretGenerators *SecretGenerators
|
||||
WebKeys struct {
|
||||
Type crypto.WebKeyConfigType
|
||||
Config struct {
|
||||
RSABits crypto.RSABits
|
||||
RSAHasher crypto.RSAHasher
|
||||
EllipticCurve crypto.EllipticCurve
|
||||
}
|
||||
}
|
||||
PasswordComplexityPolicy struct {
|
||||
MinLength uint64
|
||||
HasLowercase bool
|
||||
@@ -267,6 +275,9 @@ func setUpInstance(ctx context.Context, c *Commands, setup *InstanceSetup) (vali
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
setupSMTPSettings(c, &validations, setup.SMTPConfiguration, instanceAgg)
|
||||
if err := setupWebKeys(c, &validations, setup.zitadel.instanceID, setup); err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
setupOIDCSettings(c, &validations, setup.OIDCSettings, instanceAgg)
|
||||
setupFeatures(&validations, setup.Features, setup.zitadel.instanceID)
|
||||
setupLimits(c, &validations, limits.NewAggregate(setup.zitadel.limitsID, setup.zitadel.instanceID), setup.Limits)
|
||||
@@ -390,6 +401,29 @@ func setupFeatures(validations *[]preparation.Validation, features *InstanceFeat
|
||||
}
|
||||
}
|
||||
|
||||
func setupWebKeys(c *Commands, validations *[]preparation.Validation, instanceID string, setup *InstanceSetup) error {
|
||||
var conf crypto.WebKeyConfig
|
||||
switch setup.WebKeys.Type {
|
||||
case crypto.WebKeyConfigTypeUnspecified:
|
||||
return nil // config disabled, skip
|
||||
case crypto.WebKeyConfigTypeRSA:
|
||||
conf = &crypto.WebKeyRSAConfig{
|
||||
Bits: setup.WebKeys.Config.RSABits,
|
||||
Hasher: setup.WebKeys.Config.RSAHasher,
|
||||
}
|
||||
case crypto.WebKeyConfigTypeECDSA:
|
||||
conf = &crypto.WebKeyECDSAConfig{
|
||||
Curve: setup.WebKeys.Config.EllipticCurve,
|
||||
}
|
||||
case crypto.WebKeyConfigTypeED25519:
|
||||
conf = &crypto.WebKeyED25519Config{}
|
||||
default:
|
||||
return zerrors.ThrowInternalf(nil, "COMMAND-sieX0", "Errors.Internal unknown web key type %q", setup.WebKeys.Type)
|
||||
}
|
||||
*validations = append(*validations, c.prepareGenerateInitialWebKeys(instanceID, conf))
|
||||
return nil
|
||||
}
|
||||
|
||||
func setupOIDCSettings(commands *Commands, validations *[]preparation.Validation, oidcSettings *OIDCSettings, instanceAgg *instance.Aggregate) {
|
||||
if oidcSettings == nil {
|
||||
return
|
||||
|
@@ -3,8 +3,11 @@ package command
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/muhlemmer/gu"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/command/preparation"
|
||||
"github.com/zitadel/zitadel/internal/crypto"
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/feature"
|
||||
@@ -20,6 +23,7 @@ type InstanceFeatures struct {
|
||||
TokenExchange *bool
|
||||
Actions *bool
|
||||
ImprovedPerformance []feature.ImprovedPerformanceType
|
||||
WebKey *bool
|
||||
}
|
||||
|
||||
func (m *InstanceFeatures) isEmpty() bool {
|
||||
@@ -30,7 +34,8 @@ func (m *InstanceFeatures) isEmpty() bool {
|
||||
m.TokenExchange == nil &&
|
||||
m.Actions == nil &&
|
||||
// nil check to allow unset improvements
|
||||
m.ImprovedPerformance == nil
|
||||
m.ImprovedPerformance == nil &&
|
||||
m.WebKey == nil
|
||||
}
|
||||
|
||||
func (c *Commands) SetInstanceFeatures(ctx context.Context, f *InstanceFeatures) (*domain.ObjectDetails, error) {
|
||||
@@ -41,6 +46,9 @@ func (c *Commands) SetInstanceFeatures(ctx context.Context, f *InstanceFeatures)
|
||||
if err := c.eventstore.FilterToQueryReducer(ctx, wm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := c.setupWebKeyFeature(ctx, wm, f); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commands := wm.setCommands(ctx, f)
|
||||
if len(commands) == 0 {
|
||||
return writeModelToObjectDetails(wm.WriteModel), nil
|
||||
@@ -61,6 +69,21 @@ func prepareSetFeatures(instanceID string, f *InstanceFeatures) preparation.Vali
|
||||
}
|
||||
}
|
||||
|
||||
// setupWebKeyFeature generates the initial web keys for the instance,
|
||||
// if the feature is enabled in the request and the feature wasn't enabled already in the writeModel.
|
||||
// [Commands.GenerateInitialWebKeys] checks if keys already exist and does nothing if that's the case.
|
||||
// The default config of a RSA key with 2048 and the SHA256 hasher is assumed.
|
||||
// Users can customize this after using the webkey/v3 API.
|
||||
func (c *Commands) setupWebKeyFeature(ctx context.Context, wm *InstanceFeaturesWriteModel, f *InstanceFeatures) error {
|
||||
if !gu.Value(f.WebKey) || gu.Value(wm.WebKey) {
|
||||
return nil
|
||||
}
|
||||
return c.GenerateInitialWebKeys(ctx, &crypto.WebKeyRSAConfig{
|
||||
Bits: crypto.RSABits2048,
|
||||
Hasher: crypto.RSAHasherSHA256,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Commands) ResetInstanceFeatures(ctx context.Context) (*domain.ObjectDetails, error) {
|
||||
instanceID := authz.GetInstance(ctx).InstanceID()
|
||||
wm := NewInstanceFeaturesWriteModel(instanceID)
|
||||
|
@@ -67,6 +67,7 @@ func (m *InstanceFeaturesWriteModel) Query() *eventstore.SearchQueryBuilder {
|
||||
feature_v2.InstanceTokenExchangeEventType,
|
||||
feature_v2.InstanceActionsEventType,
|
||||
feature_v2.InstanceImprovedPerformanceEventType,
|
||||
feature_v2.InstanceWebKeyEventType,
|
||||
).
|
||||
Builder().ResourceOwner(m.ResourceOwner)
|
||||
}
|
||||
@@ -100,6 +101,9 @@ func reduceInstanceFeature(features *InstanceFeatures, key feature.Key, value an
|
||||
case feature.KeyImprovedPerformance:
|
||||
v := value.([]feature.ImprovedPerformanceType)
|
||||
features.ImprovedPerformance = v
|
||||
case feature.KeyWebKey:
|
||||
v := value.(bool)
|
||||
features.WebKey = &v
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,5 +117,6 @@ func (wm *InstanceFeaturesWriteModel) setCommands(ctx context.Context, f *Instan
|
||||
cmds = appendFeatureUpdate(ctx, cmds, aggregate, wm.UserSchema, f.UserSchema, feature_v2.InstanceUserSchemaEventType)
|
||||
cmds = appendFeatureUpdate(ctx, cmds, aggregate, wm.Actions, f.Actions, feature_v2.InstanceActionsEventType)
|
||||
cmds = appendFeatureSliceUpdate(ctx, cmds, aggregate, wm.ImprovedPerformance, f.ImprovedPerformance, feature_v2.InstanceImprovedPerformanceEventType)
|
||||
cmds = appendFeatureUpdate(ctx, cmds, aggregate, wm.WebKey, f.WebKey, feature_v2.InstanceWebKeyEventType)
|
||||
return cmds
|
||||
}
|
||||
|
@@ -10,7 +10,6 @@ import (
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/crypto"
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
"github.com/zitadel/zitadel/internal/repository/keypair"
|
||||
)
|
||||
|
||||
@@ -32,7 +31,7 @@ func (c *Commands) GenerateSigningKeyPair(ctx context.Context, algorithm string)
|
||||
_, err = c.eventstore.Push(ctx, keypair.NewAddedEvent(
|
||||
ctx,
|
||||
keyAgg,
|
||||
domain.KeyUsageSigning,
|
||||
crypto.KeyUsageSigning,
|
||||
algorithm,
|
||||
privateCrypto, publicCrypto,
|
||||
privateKeyExp, publicKeyExp))
|
||||
@@ -69,7 +68,7 @@ func (c *Commands) GenerateSAMLCACertificate(ctx context.Context, algorithm stri
|
||||
keypair.NewAddedEvent(
|
||||
ctx,
|
||||
keyAgg,
|
||||
domain.KeyUsageSAMLCA,
|
||||
crypto.KeyUsageSAMLCA,
|
||||
algorithm,
|
||||
privateCrypto, publicCrypto,
|
||||
after, after,
|
||||
@@ -115,7 +114,7 @@ func (c *Commands) GenerateSAMLResponseCertificate(ctx context.Context, algorith
|
||||
keypair.NewAddedEvent(
|
||||
ctx,
|
||||
keyAgg,
|
||||
domain.KeyUsageSAMLResponseSinging,
|
||||
crypto.KeyUsageSAMLResponseSinging,
|
||||
algorithm,
|
||||
privateCrypto, publicCrypto,
|
||||
after, after,
|
||||
@@ -160,7 +159,7 @@ func (c *Commands) GenerateSAMLMetadataCertificate(ctx context.Context, algorith
|
||||
keypair.NewAddedEvent(
|
||||
ctx,
|
||||
keyAgg,
|
||||
domain.KeyUsageSAMLMetadataSigning,
|
||||
crypto.KeyUsageSAMLMetadataSigning,
|
||||
algorithm,
|
||||
privateCrypto, publicCrypto,
|
||||
after, after),
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"github.com/zitadel/zitadel/internal/crypto"
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/repository/keypair"
|
||||
@@ -9,7 +10,7 @@ import (
|
||||
type KeyPairWriteModel struct {
|
||||
eventstore.WriteModel
|
||||
|
||||
Usage domain.KeyUsage
|
||||
Usage crypto.KeyUsage
|
||||
Algorithm string
|
||||
PrivateKey *domain.Key
|
||||
PublicKey *domain.Key
|
||||
|
188
internal/command/web_key.go
Normal file
188
internal/command/web_key.go
Normal file
@@ -0,0 +1,188 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/command/preparation"
|
||||
"github.com/zitadel/zitadel/internal/crypto"
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/repository/webkey"
|
||||
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
type WebKeyDetails struct {
|
||||
KeyID string
|
||||
ObjectDetails *domain.ObjectDetails
|
||||
}
|
||||
|
||||
// CreateWebKey creates one web key pair for the instance.
|
||||
// If the instance does not have an active key, the new key is activated.
|
||||
func (c *Commands) CreateWebKey(ctx context.Context, conf crypto.WebKeyConfig) (_ *WebKeyDetails, err error) {
|
||||
ctx, span := tracing.NewSpan(ctx)
|
||||
defer func() { span.EndWithError(err) }()
|
||||
|
||||
_, activeID, err := c.getAllWebKeys(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
addedCmd, aggregate, err := c.generateWebKeyCommand(ctx, authz.GetInstance(ctx).InstanceID(), conf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commands := []eventstore.Command{addedCmd}
|
||||
if activeID == "" {
|
||||
commands = append(commands, webkey.NewActivatedEvent(ctx, aggregate))
|
||||
}
|
||||
model := NewWebKeyWriteModel(aggregate.ID, authz.GetInstance(ctx).InstanceID())
|
||||
err = c.pushAppendAndReduce(ctx, model, commands...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &WebKeyDetails{
|
||||
KeyID: aggregate.ID,
|
||||
ObjectDetails: writeModelToObjectDetails(&model.WriteModel),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GenerateInitialWebKeys creates 2 web key pairs for the instance.
|
||||
// The first key is activated for signing use.
|
||||
// If the instance already has keys, this is noop.
|
||||
func (c *Commands) GenerateInitialWebKeys(ctx context.Context, conf crypto.WebKeyConfig) (err error) {
|
||||
ctx, span := tracing.NewSpan(ctx)
|
||||
defer func() { span.EndWithError(err) }()
|
||||
|
||||
keys, _, err := c.getAllWebKeys(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(keys) != 0 {
|
||||
return nil
|
||||
}
|
||||
commands, err := c.generateInitialWebKeysCommands(ctx, authz.GetInstance(ctx).InstanceID(), conf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = c.eventstore.Push(ctx, commands...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Commands) generateInitialWebKeysCommands(ctx context.Context, instanceID string, conf crypto.WebKeyConfig) (_ []eventstore.Command, err error) {
|
||||
ctx, span := tracing.NewSpan(ctx)
|
||||
defer func() { span.EndWithError(err) }()
|
||||
|
||||
commands := make([]eventstore.Command, 0, 3)
|
||||
for i := 0; i < 2; i++ {
|
||||
addedCmd, aggregate, err := c.generateWebKeyCommand(ctx, instanceID, conf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commands = append(commands, addedCmd)
|
||||
if i == 0 {
|
||||
commands = append(commands, webkey.NewActivatedEvent(ctx, aggregate))
|
||||
}
|
||||
}
|
||||
return commands, nil
|
||||
}
|
||||
|
||||
func (c *Commands) generateWebKeyCommand(ctx context.Context, instanceID string, conf crypto.WebKeyConfig) (_ eventstore.Command, _ *eventstore.Aggregate, err error) {
|
||||
ctx, span := tracing.NewSpan(ctx)
|
||||
defer func() { span.EndWithError(err) }()
|
||||
|
||||
keyID, err := c.idGenerator.Next()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
encryptedPrivate, public, err := c.webKeyGenerator(keyID, c.keyAlgorithm, conf)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
aggregate := webkey.NewAggregate(keyID, instanceID)
|
||||
addedCmd, err := webkey.NewAddedEvent(ctx, aggregate, encryptedPrivate, public, conf)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return addedCmd, aggregate, nil
|
||||
}
|
||||
|
||||
// ActivateWebKey activates the key identified by keyID.
|
||||
// Any previously activated key on the current instance is deactivated.
|
||||
func (c *Commands) ActivateWebKey(ctx context.Context, keyID string) (_ *domain.ObjectDetails, err error) {
|
||||
ctx, span := tracing.NewSpan(ctx)
|
||||
defer func() { span.EndWithError(err) }()
|
||||
|
||||
keys, activeID, err := c.getAllWebKeys(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if activeID == keyID {
|
||||
return writeModelToObjectDetails(
|
||||
&keys[activeID].WriteModel,
|
||||
), nil
|
||||
}
|
||||
nextActive, ok := keys[keyID]
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowNotFound(nil, "COMMAND-teiG3", "Errors.WebKey.NotFound")
|
||||
}
|
||||
|
||||
commands := make([]eventstore.Command, 0, 2)
|
||||
commands = append(commands, webkey.NewActivatedEvent(ctx,
|
||||
webkey.AggregateFromWriteModel(ctx, &nextActive.WriteModel),
|
||||
))
|
||||
if activeID != "" {
|
||||
commands = append(commands, webkey.NewDeactivatedEvent(ctx,
|
||||
webkey.AggregateFromWriteModel(ctx, &keys[activeID].WriteModel),
|
||||
))
|
||||
}
|
||||
err = c.pushAppendAndReduce(ctx, nextActive, commands...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return writeModelToObjectDetails(&nextActive.WriteModel), nil
|
||||
}
|
||||
|
||||
// getAllWebKeys searches for all web keys on the instance and returns a map of key IDs.
|
||||
// activeID is the id of the currently active key.
|
||||
func (c *Commands) getAllWebKeys(ctx context.Context) (_ map[string]*WebKeyWriteModel, activeID string, err error) {
|
||||
ctx, span := tracing.NewSpan(ctx)
|
||||
defer func() { span.EndWithError(err) }()
|
||||
|
||||
models := newWebKeyWriteModels(authz.GetInstance(ctx).InstanceID())
|
||||
if err = c.eventstore.FilterToQueryReducer(ctx, models); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return models.keys, models.activeID, nil
|
||||
}
|
||||
|
||||
func (c *Commands) DeleteWebKey(ctx context.Context, keyID string) (_ *domain.ObjectDetails, err error) {
|
||||
ctx, span := tracing.NewSpan(ctx)
|
||||
defer func() { span.EndWithError(err) }()
|
||||
|
||||
model := NewWebKeyWriteModel(keyID, authz.GetInstance(ctx).InstanceID())
|
||||
if err = c.eventstore.FilterToQueryReducer(ctx, model); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if model.State == domain.WebKeyStateUnspecified {
|
||||
return nil, zerrors.ThrowNotFound(nil, "COMMAND-ooCa7", "Errors.WebKey.NotFound")
|
||||
}
|
||||
if model.State == domain.WebKeyStateActive {
|
||||
return nil, zerrors.ThrowPreconditionFailed(nil, "COMMAND-Chai1", "Errors.WebKey.ActiveDelete")
|
||||
}
|
||||
err = c.pushAppendAndReduce(ctx, model, webkey.NewRemovedEvent(ctx,
|
||||
webkey.AggregateFromWriteModel(ctx, &model.WriteModel),
|
||||
))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return writeModelToObjectDetails(&model.WriteModel), nil
|
||||
}
|
||||
|
||||
func (c *Commands) prepareGenerateInitialWebKeys(instanceID string, conf crypto.WebKeyConfig) preparation.Validation {
|
||||
return func() (preparation.CreateCommands, error) {
|
||||
return func(ctx context.Context, _ preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
||||
return c.generateInitialWebKeysCommands(ctx, instanceID, conf)
|
||||
}, nil
|
||||
}
|
||||
}
|
131
internal/command/web_key_model.go
Normal file
131
internal/command/web_key_model.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"github.com/go-jose/go-jose/v4"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/crypto"
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/repository/webkey"
|
||||
)
|
||||
|
||||
type WebKeyWriteModel struct {
|
||||
eventstore.WriteModel
|
||||
State domain.WebKeyState
|
||||
PrivateKey *crypto.CryptoValue
|
||||
PublicKey *jose.JSONWebKey
|
||||
}
|
||||
|
||||
func NewWebKeyWriteModel(keyID, resourceOwner string) *WebKeyWriteModel {
|
||||
return &WebKeyWriteModel{
|
||||
WriteModel: eventstore.WriteModel{
|
||||
AggregateID: keyID,
|
||||
ResourceOwner: resourceOwner,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (wm *WebKeyWriteModel) AppendEvents(events ...eventstore.Event) {
|
||||
wm.WriteModel.AppendEvents(events...)
|
||||
}
|
||||
|
||||
func (wm *WebKeyWriteModel) Reduce() error {
|
||||
for _, event := range wm.Events {
|
||||
if event.Aggregate().ID != wm.AggregateID {
|
||||
continue
|
||||
}
|
||||
switch e := event.(type) {
|
||||
case *webkey.AddedEvent:
|
||||
wm.State = domain.WebKeyStateInitial
|
||||
wm.PrivateKey = e.PrivateKey
|
||||
wm.PublicKey = e.PublicKey
|
||||
case *webkey.ActivatedEvent:
|
||||
wm.State = domain.WebKeyStateActive
|
||||
case *webkey.DeactivatedEvent:
|
||||
wm.State = domain.WebKeyStateInactive
|
||||
case *webkey.RemovedEvent:
|
||||
wm.State = domain.WebKeyStateRemoved
|
||||
wm.PrivateKey = nil
|
||||
wm.PublicKey = nil
|
||||
}
|
||||
}
|
||||
return wm.WriteModel.Reduce()
|
||||
}
|
||||
|
||||
func (wm *WebKeyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
||||
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
||||
ResourceOwner(wm.ResourceOwner).
|
||||
AddQuery().
|
||||
AggregateTypes(webkey.AggregateType).
|
||||
AggregateIDs(wm.AggregateID).
|
||||
EventTypes(
|
||||
webkey.AddedEventType,
|
||||
webkey.ActivatedEventType,
|
||||
webkey.DeactivatedEventType,
|
||||
webkey.RemovedEventType,
|
||||
).
|
||||
Builder()
|
||||
}
|
||||
|
||||
type webKeyWriteModels struct {
|
||||
resourceOwner string
|
||||
events []eventstore.Event
|
||||
keys map[string]*WebKeyWriteModel
|
||||
activeID string
|
||||
}
|
||||
|
||||
func newWebKeyWriteModels(resourceOwner string) *webKeyWriteModels {
|
||||
return &webKeyWriteModels{
|
||||
resourceOwner: resourceOwner,
|
||||
keys: make(map[string]*WebKeyWriteModel),
|
||||
}
|
||||
}
|
||||
|
||||
func (models *webKeyWriteModels) AppendEvents(events ...eventstore.Event) {
|
||||
models.events = append(models.events, events...)
|
||||
}
|
||||
|
||||
func (models *webKeyWriteModels) Reduce() error {
|
||||
for _, event := range models.events {
|
||||
aggregate := event.Aggregate()
|
||||
if models.keys[aggregate.ID] == nil {
|
||||
models.keys[aggregate.ID] = NewWebKeyWriteModel(aggregate.ID, aggregate.ResourceOwner)
|
||||
}
|
||||
|
||||
switch event.(type) {
|
||||
case *webkey.AddedEvent:
|
||||
break
|
||||
case *webkey.ActivatedEvent:
|
||||
models.activeID = aggregate.ID
|
||||
case *webkey.DeactivatedEvent:
|
||||
if models.activeID == aggregate.ID {
|
||||
models.activeID = ""
|
||||
}
|
||||
case *webkey.RemovedEvent:
|
||||
delete(models.keys, aggregate.ID)
|
||||
continue
|
||||
}
|
||||
|
||||
model := models.keys[aggregate.ID]
|
||||
model.AppendEvents(event)
|
||||
if err := model.Reduce(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
models.events = models.events[0:0]
|
||||
return nil
|
||||
}
|
||||
|
||||
func (models *webKeyWriteModels) Query() *eventstore.SearchQueryBuilder {
|
||||
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
||||
ResourceOwner(models.resourceOwner).
|
||||
AddQuery().
|
||||
AggregateTypes(webkey.AggregateType).
|
||||
EventTypes(
|
||||
webkey.AddedEventType,
|
||||
webkey.ActivatedEventType,
|
||||
webkey.DeactivatedEventType,
|
||||
webkey.RemovedEventType,
|
||||
).
|
||||
Builder()
|
||||
}
|
754
internal/command/web_key_test.go
Normal file
754
internal/command/web_key_test.go
Normal file
@@ -0,0 +1,754 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/go-jose/go-jose/v4"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/crypto"
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/id"
|
||||
id_mock "github.com/zitadel/zitadel/internal/id/mock"
|
||||
"github.com/zitadel/zitadel/internal/repository/webkey"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
func TestCommands_CreateWebKey(t *testing.T) {
|
||||
ctx := authz.NewMockContextWithPermissions("instance1", "org1", "user1", nil)
|
||||
key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
|
||||
require.NoError(t, err)
|
||||
|
||||
type fields struct {
|
||||
eventstore func(*testing.T) *eventstore.Eventstore
|
||||
idGenerator id.Generator
|
||||
webKeyGenerator func(keyID string, alg crypto.EncryptionAlgorithm, genConfig crypto.WebKeyConfig) (encryptedPrivate *crypto.CryptoValue, public *jose.JSONWebKey, err error)
|
||||
}
|
||||
type args struct {
|
||||
conf crypto.WebKeyConfig
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want *WebKeyDetails
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "filter error",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilterError(io.ErrClosedPipe),
|
||||
),
|
||||
},
|
||||
args: args{
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
},
|
||||
wantErr: io.ErrClosedPipe,
|
||||
},
|
||||
{
|
||||
name: "generate error",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(),
|
||||
),
|
||||
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "key1"),
|
||||
webKeyGenerator: func(string, crypto.EncryptionAlgorithm, crypto.WebKeyConfig) (*crypto.CryptoValue, *jose.JSONWebKey, error) {
|
||||
return nil, nil, io.ErrClosedPipe
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
},
|
||||
wantErr: io.ErrClosedPipe,
|
||||
},
|
||||
{
|
||||
name: "generate key, ok",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(
|
||||
eventFromEventPusher(mustNewWebkeyAddedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
&crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
},
|
||||
&jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: "key1",
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
},
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
)),
|
||||
eventFromEventPusher(webkey.NewActivatedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
)),
|
||||
),
|
||||
expectPush(
|
||||
mustNewWebkeyAddedEvent(ctx,
|
||||
webkey.NewAggregate("key2", "instance1"),
|
||||
&crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
},
|
||||
&jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: "key2",
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
},
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "key2"),
|
||||
webKeyGenerator: func(keyID string, _ crypto.EncryptionAlgorithm, _ crypto.WebKeyConfig) (*crypto.CryptoValue, *jose.JSONWebKey, error) {
|
||||
return &crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
}, &jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: keyID,
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
},
|
||||
want: &WebKeyDetails{
|
||||
KeyID: "key2",
|
||||
ObjectDetails: &domain.ObjectDetails{
|
||||
ResourceOwner: "instance1",
|
||||
ID: "key2",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "generate and activate key, ok",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(),
|
||||
expectPush(
|
||||
mustNewWebkeyAddedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
&crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
},
|
||||
&jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: "key1",
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
},
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
),
|
||||
webkey.NewActivatedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
),
|
||||
),
|
||||
),
|
||||
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "key1"),
|
||||
webKeyGenerator: func(keyID string, _ crypto.EncryptionAlgorithm, _ crypto.WebKeyConfig) (*crypto.CryptoValue, *jose.JSONWebKey, error) {
|
||||
return &crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
}, &jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: keyID,
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
},
|
||||
want: &WebKeyDetails{
|
||||
KeyID: "key1",
|
||||
ObjectDetails: &domain.ObjectDetails{
|
||||
ResourceOwner: "instance1",
|
||||
ID: "key1",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := &Commands{
|
||||
eventstore: tt.fields.eventstore(t),
|
||||
idGenerator: tt.fields.idGenerator,
|
||||
webKeyGenerator: tt.fields.webKeyGenerator,
|
||||
}
|
||||
got, err := c.CreateWebKey(ctx, tt.args.conf)
|
||||
require.ErrorIs(t, err, tt.wantErr)
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommands_GenerateInitialWebKeys(t *testing.T) {
|
||||
ctx := authz.NewMockContextWithPermissions("instance1", "org1", "user1", nil)
|
||||
key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
|
||||
require.NoError(t, err)
|
||||
|
||||
type fields struct {
|
||||
eventstore func(*testing.T) *eventstore.Eventstore
|
||||
idGenerator id.Generator
|
||||
webKeyGenerator func(keyID string, alg crypto.EncryptionAlgorithm, genConfig crypto.WebKeyConfig) (encryptedPrivate *crypto.CryptoValue, public *jose.JSONWebKey, err error)
|
||||
}
|
||||
type args struct {
|
||||
conf crypto.WebKeyConfig
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "filter error",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilterError(io.ErrClosedPipe),
|
||||
),
|
||||
},
|
||||
args: args{
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
},
|
||||
wantErr: io.ErrClosedPipe,
|
||||
},
|
||||
{
|
||||
name: "key found, noop",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(
|
||||
eventFromEventPusher(mustNewWebkeyAddedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
&crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
},
|
||||
&jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: "key1",
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
},
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
)),
|
||||
eventFromEventPusher(webkey.NewActivatedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
)),
|
||||
),
|
||||
),
|
||||
},
|
||||
args: args{
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "id generator error",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(expectFilter()),
|
||||
idGenerator: id_mock.NewIDGeneratorExpectError(t, io.ErrUnexpectedEOF),
|
||||
},
|
||||
args: args{
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
},
|
||||
wantErr: io.ErrUnexpectedEOF,
|
||||
},
|
||||
{
|
||||
name: "keys generated and activated",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(),
|
||||
expectPush(
|
||||
mustNewWebkeyAddedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
&crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
},
|
||||
&jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: "key1",
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
},
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
),
|
||||
webkey.NewActivatedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
),
|
||||
mustNewWebkeyAddedEvent(ctx,
|
||||
webkey.NewAggregate("key2", "instance1"),
|
||||
&crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
},
|
||||
&jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: "key2",
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
},
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
idGenerator: id_mock.NewIDGeneratorExpectIDs(t, "key1", "key2"),
|
||||
webKeyGenerator: func(keyID string, _ crypto.EncryptionAlgorithm, _ crypto.WebKeyConfig) (*crypto.CryptoValue, *jose.JSONWebKey, error) {
|
||||
return &crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
}, &jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: keyID,
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := &Commands{
|
||||
eventstore: tt.fields.eventstore(t),
|
||||
idGenerator: tt.fields.idGenerator,
|
||||
webKeyGenerator: tt.fields.webKeyGenerator,
|
||||
}
|
||||
err := c.GenerateInitialWebKeys(ctx, tt.args.conf)
|
||||
require.ErrorIs(t, err, tt.wantErr)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommands_ActivateWebKey(t *testing.T) {
|
||||
ctx := authz.NewMockContextWithPermissions("instance1", "org1", "user1", nil)
|
||||
key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
|
||||
require.NoError(t, err)
|
||||
|
||||
type fields struct {
|
||||
eventstore func(*testing.T) *eventstore.Eventstore
|
||||
webKeyGenerator func(keyID string, alg crypto.EncryptionAlgorithm, genConfig crypto.WebKeyConfig) (encryptedPrivate *crypto.CryptoValue, public *jose.JSONWebKey, err error)
|
||||
}
|
||||
type args struct {
|
||||
keyID string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want *domain.ObjectDetails
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "filter error",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilterError(io.ErrClosedPipe),
|
||||
),
|
||||
},
|
||||
args: args{"key2"},
|
||||
wantErr: io.ErrClosedPipe,
|
||||
},
|
||||
{
|
||||
name: "no changes",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(
|
||||
eventFromEventPusher(mustNewWebkeyAddedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
&crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
},
|
||||
&jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: "key1",
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
},
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
)),
|
||||
eventFromEventPusher(webkey.NewActivatedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
)),
|
||||
),
|
||||
),
|
||||
},
|
||||
args: args{"key1"},
|
||||
want: &domain.ObjectDetails{
|
||||
ResourceOwner: "instance1",
|
||||
ID: "key1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "not found error",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(
|
||||
eventFromEventPusher(mustNewWebkeyAddedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
&crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
},
|
||||
&jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: "key1",
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
},
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
)),
|
||||
eventFromEventPusher(webkey.NewActivatedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
)),
|
||||
),
|
||||
),
|
||||
},
|
||||
args: args{"key2"},
|
||||
wantErr: zerrors.ThrowNotFound(nil, "COMMAND-teiG3", "Errors.WebKey.NotFound"),
|
||||
},
|
||||
{
|
||||
name: "activate next, de-activate old, ok",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(
|
||||
eventFromEventPusher(mustNewWebkeyAddedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
&crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
},
|
||||
&jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: "key1",
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
},
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
)),
|
||||
eventFromEventPusher(webkey.NewActivatedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
)),
|
||||
eventFromEventPusher(mustNewWebkeyAddedEvent(ctx,
|
||||
webkey.NewAggregate("key2", "instance1"),
|
||||
&crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
},
|
||||
&jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: "key2",
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
},
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
)),
|
||||
),
|
||||
expectPush(
|
||||
webkey.NewActivatedEvent(ctx,
|
||||
webkey.NewAggregate("key2", "instance1"),
|
||||
),
|
||||
webkey.NewDeactivatedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
),
|
||||
),
|
||||
),
|
||||
},
|
||||
args: args{"key2"},
|
||||
want: &domain.ObjectDetails{
|
||||
ResourceOwner: "instance1",
|
||||
ID: "key2",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "activate next, ok",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(
|
||||
eventFromEventPusher(mustNewWebkeyAddedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
&crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
},
|
||||
&jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: "key1",
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
},
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
)),
|
||||
),
|
||||
expectPush(
|
||||
webkey.NewActivatedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
),
|
||||
),
|
||||
),
|
||||
},
|
||||
args: args{"key1"},
|
||||
want: &domain.ObjectDetails{
|
||||
ResourceOwner: "instance1",
|
||||
ID: "key1",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := &Commands{
|
||||
eventstore: tt.fields.eventstore(t),
|
||||
webKeyGenerator: tt.fields.webKeyGenerator,
|
||||
}
|
||||
got, err := c.ActivateWebKey(ctx, tt.args.keyID)
|
||||
require.ErrorIs(t, err, tt.wantErr)
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommands_DeleteWebKey(t *testing.T) {
|
||||
ctx := authz.NewMockContextWithPermissions("instance1", "org1", "user1", nil)
|
||||
key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
|
||||
require.NoError(t, err)
|
||||
|
||||
type fields struct {
|
||||
eventstore func(*testing.T) *eventstore.Eventstore
|
||||
}
|
||||
type args struct {
|
||||
keyID string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want *domain.ObjectDetails
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "filter error",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilterError(io.ErrClosedPipe),
|
||||
),
|
||||
},
|
||||
args: args{"key1"},
|
||||
wantErr: io.ErrClosedPipe,
|
||||
},
|
||||
{
|
||||
name: "not found error",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(),
|
||||
),
|
||||
},
|
||||
args: args{"key1"},
|
||||
wantErr: zerrors.ThrowNotFound(nil, "COMMAND-ooCa7", "Errors.WebKey.NotFound"),
|
||||
},
|
||||
{
|
||||
name: "key active error",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(
|
||||
eventFromEventPusher(mustNewWebkeyAddedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
&crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
},
|
||||
&jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: "key1",
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
},
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
)),
|
||||
eventFromEventPusher(webkey.NewActivatedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
)),
|
||||
),
|
||||
),
|
||||
},
|
||||
args: args{"key1"},
|
||||
wantErr: zerrors.ThrowPreconditionFailed(nil, "COMMAND-Chai1", "Errors.WebKey.ActiveDelete"),
|
||||
},
|
||||
{
|
||||
name: "delete deactivated key",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(
|
||||
eventFromEventPusher(mustNewWebkeyAddedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
&crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
},
|
||||
&jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: "key1",
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
},
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
)),
|
||||
eventFromEventPusher(webkey.NewActivatedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
)),
|
||||
eventFromEventPusher(mustNewWebkeyAddedEvent(ctx,
|
||||
webkey.NewAggregate("key2", "instance1"),
|
||||
&crypto.CryptoValue{
|
||||
CryptoType: crypto.TypeEncryption,
|
||||
Algorithm: "alg",
|
||||
KeyID: "encKey",
|
||||
Crypted: []byte("crypted"),
|
||||
},
|
||||
&jose.JSONWebKey{
|
||||
Key: &key.PublicKey,
|
||||
KeyID: "key2",
|
||||
Algorithm: string(jose.ES384),
|
||||
Use: crypto.KeyUsageSigning.String(),
|
||||
},
|
||||
&crypto.WebKeyECDSAConfig{
|
||||
Curve: crypto.EllipticCurveP384,
|
||||
},
|
||||
)),
|
||||
eventFromEventPusher(webkey.NewActivatedEvent(ctx,
|
||||
webkey.NewAggregate("key2", "instance1"),
|
||||
)),
|
||||
eventFromEventPusher(webkey.NewDeactivatedEvent(ctx,
|
||||
webkey.NewAggregate("key1", "instance1"),
|
||||
)),
|
||||
),
|
||||
expectPush(
|
||||
webkey.NewRemovedEvent(ctx, webkey.NewAggregate("key1", "instance1")),
|
||||
),
|
||||
),
|
||||
},
|
||||
args: args{"key1"},
|
||||
want: &domain.ObjectDetails{
|
||||
ResourceOwner: "instance1",
|
||||
ID: "key1",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := &Commands{
|
||||
eventstore: tt.fields.eventstore(t),
|
||||
}
|
||||
got, err := c.DeleteWebKey(ctx, tt.args.keyID)
|
||||
require.ErrorIs(t, err, tt.wantErr)
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func mustNewWebkeyAddedEvent(
|
||||
ctx context.Context,
|
||||
aggregate *eventstore.Aggregate,
|
||||
privateKey *crypto.CryptoValue,
|
||||
publicKey *jose.JSONWebKey,
|
||||
config crypto.WebKeyConfig) *webkey.AddedEvent {
|
||||
event, err := webkey.NewAddedEvent(ctx, aggregate, privateKey, publicKey, config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return event
|
||||
}
|
Reference in New Issue
Block a user