mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-08 23:25:12 +00:00
feat(action v2): add JWT and JWE payload type options (#11196)
# Which Problems Are Solved The payload in actions V2 is currently sent as JSON to the target endpoint. It might get exposed to intermediary infrastructure or logging systems. For these scenarios there needs to be an application-layer encryption, where the provider of the endpoint can define an key to be used for the encryption. # How the Problems Are Solved - Added an additional option to the target to specify the payload type: `JSON` (current and default), `JWT`, `JWE` (api and console) - added endpoints to upload and manage public keys (to be used for encryption) for a target - updated all action v2 executions (interceptors, oidc, saml, ...) to provide the `GetActiveSigningWebKey` from queries - implemented jwt and jwe in the exections incl. refactoring of code and tests - changes to the authn_keys table: - added a `fingerprint` column - dropped not null constraint on expiration - moved the `GetSignerOnce` into its own package to prevent circular dependencies # Additional Changes None # Additional Context closes #11061 --------- Co-authored-by: Marco A. <[email protected]> Co-authored-by: conblem <[email protected]> Co-authored-by: Silvan <[email protected]>
This commit is contained in:
co-authored by
Marco A.
conblem
Silvan
parent
72c8f0192c
commit
791d0587aa
@@ -3,15 +3,22 @@ package execution
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"io"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-jose/go-jose/v4"
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
zhttp "github.com/zitadel/zitadel/internal/api/http"
|
||||
"github.com/zitadel/zitadel/internal/api/oidc/sign"
|
||||
"github.com/zitadel/zitadel/internal/crypto"
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
target_domain "github.com/zitadel/zitadel/internal/execution/target"
|
||||
@@ -27,20 +34,29 @@ type ContextInfo interface {
|
||||
SetHTTPResponseBody([]byte) error
|
||||
}
|
||||
|
||||
type GetActiveSigningWebKey func(ctx context.Context) (*jose.JSONWebKey, error)
|
||||
|
||||
// CallTargets call a list of targets in order with handling of error and responses
|
||||
func CallTargets(
|
||||
ctx context.Context,
|
||||
targets []target_domain.Target,
|
||||
info ContextInfo,
|
||||
alg crypto.EncryptionAlgorithm,
|
||||
activeSigningKey GetActiveSigningWebKey,
|
||||
) (_ interface{}, err error) {
|
||||
ctx, span := tracing.NewSpan(ctx)
|
||||
defer func() { span.EndWithError(err) }()
|
||||
|
||||
// We make sure the signer and its key are only fetched once per CallTargets call.
|
||||
signerOnce := sign.GetSignerOnce(activeSigningKey)
|
||||
// Create a map to cache encrypters by key ID to avoid recreating them for each target.
|
||||
encrypters := &sync.Map{}
|
||||
|
||||
for _, target := range targets {
|
||||
// call the type of target
|
||||
resp, err := CallTarget(ctx, target, info, alg)
|
||||
resp, err := CallTarget(ctx, target, info, alg, signerOnce, encrypters)
|
||||
// handle error if interrupt is set
|
||||
logging.WithFields("instanceID", authz.GetInstance(ctx).InstanceID(), "target", target.GetTargetID()).OnError(err).Error("error calling target")
|
||||
if err != nil && target.IsInterruptOnError() {
|
||||
return nil, err
|
||||
}
|
||||
@@ -64,6 +80,8 @@ func CallTarget(
|
||||
target target_domain.Target,
|
||||
info ContextInfoRequest,
|
||||
alg crypto.EncryptionAlgorithm,
|
||||
signerOnce sign.SignerFunc,
|
||||
encrypters *sync.Map,
|
||||
) (res []byte, err error) {
|
||||
ctx, span := tracing.NewSpan(ctx)
|
||||
defer func() { span.EndWithError(err) }()
|
||||
@@ -72,26 +90,137 @@ func CallTarget(
|
||||
if err != nil {
|
||||
return nil, zerrors.ThrowInternal(err, "EXEC-thiiCh5b", "Errors.Internal")
|
||||
}
|
||||
body, err := payload(ctx, info.GetHTTPRequestBody(), target, signerOnce, encrypters)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch target.GetTargetType() {
|
||||
// get request, ignore response and return request and error for handling in list of targets
|
||||
case target_domain.TargetTypeWebhook:
|
||||
return nil, webhook(ctx, target.GetEndpoint(), target.GetTimeout(), info.GetHTTPRequestBody(), signingKey)
|
||||
return nil, webhook(ctx, target.GetEndpoint(), target.GetTimeout(), body, signingKey)
|
||||
// get request, return response and error
|
||||
case target_domain.TargetTypeCall:
|
||||
return Call(ctx, target.GetEndpoint(), target.GetTimeout(), info.GetHTTPRequestBody(), signingKey)
|
||||
return Call(ctx, target.GetEndpoint(), target.GetTimeout(), body, signingKey)
|
||||
case target_domain.TargetTypeAsync:
|
||||
go func(ctx context.Context, target target_domain.Target, info []byte) {
|
||||
if _, err := Call(ctx, target.GetEndpoint(), target.GetTimeout(), info, signingKey); err != nil {
|
||||
logging.WithFields("target", target.GetTargetID()).OnError(err).Info(err)
|
||||
}
|
||||
}(context.WithoutCancel(ctx), target, info.GetHTTPRequestBody())
|
||||
}(context.WithoutCancel(ctx), target, body)
|
||||
return nil, nil
|
||||
default:
|
||||
return nil, zerrors.ThrowInternal(nil, "EXEC-auqnansr2m", "Errors.Execution.Unknown")
|
||||
}
|
||||
}
|
||||
|
||||
func payload(ctx context.Context, payload []byte, target target_domain.Target, signerOnce sign.SignerFunc, encrypters *sync.Map) ([]byte, error) {
|
||||
switch target.GetPayloadType() {
|
||||
case target_domain.PayloadTypeUnspecified,
|
||||
target_domain.PayloadTypeJSON:
|
||||
return payload, nil
|
||||
case target_domain.PayloadTypeJWT:
|
||||
return payloadJWT(ctx, payload, signerOnce)
|
||||
case target_domain.PayloadTypeJWE:
|
||||
return payloadJWE(ctx, payload, target, signerOnce, encrypters)
|
||||
default:
|
||||
return payload, nil
|
||||
}
|
||||
}
|
||||
|
||||
func payloadJWT(ctx context.Context, payload []byte, signerOnce sign.SignerFunc) ([]byte, error) {
|
||||
signer, _, err := signerOnce(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sig, err := signer.Sign(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := sig.CompactSerialize()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []byte(data), nil
|
||||
}
|
||||
|
||||
func loadEncrypter(target target_domain.Target, encrypters *sync.Map) (jose.Encrypter, error) {
|
||||
if encrypter, ok := encrypters.Load(target.GetEncryptionKeyID()); ok {
|
||||
return encrypter.(jose.Encrypter), nil
|
||||
}
|
||||
encryptionKey := target.GetEncryptionKey()
|
||||
if len(encryptionKey) == 0 {
|
||||
return nil, zerrors.ThrowInternal(nil, "EXEC-2n8fhs7g", "Errors.Execution.MissingEncryptionKey")
|
||||
}
|
||||
publicKey, algorithm, err := publicKeyFromBytes(encryptionKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
encrypter, err := jose.NewEncrypter(
|
||||
jose.A256GCM,
|
||||
jose.Recipient{
|
||||
Algorithm: algorithm,
|
||||
Key: publicKey,
|
||||
KeyID: target.GetEncryptionKeyID(),
|
||||
},
|
||||
(&jose.EncrypterOptions{}).
|
||||
WithType("JWT").
|
||||
WithContentType("JWT"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
encrypters.Store(target.GetEncryptionKeyID(), encrypter)
|
||||
return encrypter, nil
|
||||
}
|
||||
|
||||
func payloadJWE(
|
||||
ctx context.Context,
|
||||
payload []byte,
|
||||
target target_domain.Target,
|
||||
signerOnce sign.SignerFunc,
|
||||
encrypters *sync.Map,
|
||||
) ([]byte, error) {
|
||||
payload, err := payloadJWT(ctx, payload, signerOnce)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
encrypter, err := loadEncrypter(target, encrypters)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
enc, err := encrypter.Encrypt(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
crypted, err := enc.CompactSerialize()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []byte(crypted), nil
|
||||
}
|
||||
|
||||
func publicKeyFromBytes(data []byte) (any, jose.KeyAlgorithm, error) {
|
||||
block, _ := pem.Decode(data)
|
||||
if block == nil {
|
||||
return nil, "", zerrors.ThrowInternal(nil, "EXEC-3n8fhs7g", "Errors.Execution.InvalidPublicKey")
|
||||
}
|
||||
publicKey, err := x509.ParsePKIXPublicKey(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
switch pk := publicKey.(type) {
|
||||
case *rsa.PublicKey:
|
||||
return pk, jose.RSA_OAEP_256, nil
|
||||
case *ecdsa.PublicKey:
|
||||
return pk, jose.ECDH_ES_A256KW, nil
|
||||
default:
|
||||
return nil, "", zerrors.ThrowInternal(nil, "EXEC-NKJe2", "Errors.Execution.InvalidPublicKey")
|
||||
}
|
||||
}
|
||||
|
||||
// webhook call a webhook, ignore the response but return the errror
|
||||
func webhook(ctx context.Context, url string, timeout time.Duration, body []byte, signingKey string) error {
|
||||
_, err := Call(ctx, url, timeout, body, signingKey)
|
||||
|
||||
Reference in New Issue
Block a user