Skip to content

Commit

Permalink
feat(secret): add app id and key to secret id (#1764)
Browse files Browse the repository at this point in the history
  • Loading branch information
hekike authored Oct 29, 2024
1 parent f429013 commit af1dcce
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 39 deletions.
20 changes: 7 additions & 13 deletions openmeter/app/stripe/adapter/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
appstripecustomerdb "github.com/openmeterio/openmeter/openmeter/ent/db/appstripecustomer"
secretentity "github.com/openmeterio/openmeter/openmeter/secret/entity"
"github.com/openmeterio/openmeter/pkg/framework/entutils"
"github.com/openmeterio/openmeter/pkg/models"
)

var _ appstripe.AppStripeAdapter = (*adapter)(nil)
Expand Down Expand Up @@ -130,12 +129,12 @@ func (a adapter) GetWebhookSecret(ctx context.Context, input appstripeentity.Get
}

// Get the webhook secret
secret, err := a.secretService.GetAppSecret(ctx, secretentity.GetAppSecretInput{
NamespacedID: models.NamespacedID{
Namespace: stripeApp.Namespace,
ID: stripeApp.WebhookSecret,
},
})
appID := appentitybase.AppID{
Namespace: stripeApp.Namespace,
ID: stripeApp.ID,
}

secret, err := a.secretService.GetAppSecret(ctx, secretentity.NewSecretID(appID, stripeApp.WebhookSecret, appstripeentity.WebhookSecretKey))
if err != nil {
return secretentity.Secret{}, fmt.Errorf("failed to get webhook secret: %w", err)
}
Expand Down Expand Up @@ -323,12 +322,7 @@ func (a adapter) CreateCheckoutSession(ctx context.Context, input appstripeentit
input.StripeCustomerID = &stripeCustomerId

// Get Stripe API Key
apiKeySecret, err := repo.secretService.GetAppSecret(ctx, secretentity.GetAppSecretInput{
NamespacedID: models.NamespacedID{
Namespace: stripeApp.Namespace,
ID: stripeApp.APIKey,
},
})
apiKeySecret, err := repo.secretService.GetAppSecret(ctx, secretentity.NewSecretID(appID, stripeApp.APIKey, appstripeentity.APIKeySecretKey))
if err != nil {
return appstripeentity.CreateCheckoutSessionOutput{}, fmt.Errorf("failed to get stripe api key secret: %w", err)
}
Expand Down
8 changes: 1 addition & 7 deletions openmeter/app/stripe/entity/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
customerentity "github.com/openmeterio/openmeter/openmeter/customer/entity"
"github.com/openmeterio/openmeter/openmeter/secret"
secretentity "github.com/openmeterio/openmeter/openmeter/secret/entity"
"github.com/openmeterio/openmeter/pkg/models"
)

const (
Expand Down Expand Up @@ -91,12 +90,7 @@ func (a App) ValidateCustomer(ctx context.Context, customer *customerentity.Cust
}

// Get Stripe API Key
apiKeySecret, err := a.SecretService.GetAppSecret(ctx, secretentity.GetAppSecretInput{
NamespacedID: models.NamespacedID{
Namespace: stripeAppData.APIKey.Namespace,
ID: stripeAppData.APIKey.ID,
},
})
apiKeySecret, err := a.SecretService.GetAppSecret(ctx, secretentity.NewSecretID(a.GetID(), stripeAppData.APIKey.ID, appstripeentity.APIKeySecretKey))
if err != nil {
return fmt.Errorf("failed to get stripe api key secret: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions openmeter/secret/adapter/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func (a adapter) CreateAppSecret(ctx context.Context, input secretentity.CreateA
Namespace: input.AppID.Namespace,
ID: input.Value,
},
// AppID: input.AppID,
// Key: input.Key,
AppID: input.AppID,
Key: input.Key,
}, nil
}

Expand All @@ -29,8 +29,8 @@ func (a adapter) GetAppSecret(ctx context.Context, input secretentity.GetAppSecr
Namespace: input.Namespace,
ID: input.ID,
},
// AppID: input.AppID,
// Key: input.Key,
AppID: input.AppID,
Key: input.Key,
},
Value: input.ID,
}, nil
Expand Down
30 changes: 15 additions & 15 deletions openmeter/secret/entity/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
// SecretID represents a secret identifier.
type SecretID struct {
models.NamespacedID
// AppID appentitybase.AppID
// Key string
AppID appentitybase.AppID
Key string
}

func NewSecretID(appID appentitybase.AppID, id string, key string) SecretID {
Expand All @@ -21,29 +21,29 @@ func NewSecretID(appID appentitybase.AppID, id string, key string) SecretID {
Namespace: appID.Namespace,
ID: id,
},
// AppID: appID,
// Key: key,
AppID: appID,
Key: key,
}
}

func (i SecretID) Validate() error {
if err := i.NamespacedID.Validate(); err != nil {
return ValidationError{
Err: fmt.Errorf("secret %w", err),
Err: fmt.Errorf("secret: %w", err),
}
}

// if err := i.AppID.Validate(); err != nil {
// return ValidationError{
// Err: fmt.Errorf("secret app id %w", err),
// }
// }
if err := i.AppID.Validate(); err != nil {
return ValidationError{
Err: fmt.Errorf("secret app id: %w", err),
}
}

// if i.Key == "" {
// return ValidationError{
// Err: errors.New("secret key is required"),
// }
// }
if i.Key == "" {
return ValidationError{
Err: errors.New("secret key is required"),
}
}

return nil
}
Expand Down

0 comments on commit af1dcce

Please sign in to comment.