Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ The gateway emits one ECS-formatted JSON completion event per exchange. It conta

The gateway is an internal Kubernetes `ClusterIP` Service in SIT. Authentication is enabled by default and validates JWTs before enforcing these scopes. A local test must explicitly set `GATEWAY_SECURITY_ENABLED=false` to use the bypass configuration; do not use that bypass in a deployed environment.

- `admin.internal` for `/admin/**` and centralized API documentation.
- `admin.internal` for `/admin/**`, centralized API documentation, and administrative access to customer/account resources.
- `customer.self` for a customer profile whose UUID matches the JWT `sub` claim.
- `account.self` for account resources owned by the customer UUID in the JWT `sub` claim.
- `mfa.internal` for `/api/v1/mfa/**`.
- `transfer.internal` for `/internal/v1/transfer-workflows/**`.
- `payment.internal` for `/internal/v1/payment-instructions/**`.

Customer and account APIs require an authenticated token. Health endpoints and Auth login remain public. Requests that do not authenticate receive `401` Problem Details; authenticated requests without the required scope receive `403` Problem Details.
Customer and account APIs require an ownership scope, not merely an authenticated token. Health endpoints and Auth login remain public. Requests that do not authenticate receive `401` Problem Details; authenticated requests without the required scope or ownership receive `403` Problem Details. The current SIT fixture is the `transfer-orchestrator` administrative identity and retains `admin.internal`; subject-to-customer UUID mapping is a temporary boundary until a customer identity provider supplies explicit customer claims.

SIT uses a base64-encoded HMAC secret supplied through the Kubernetes `auth-service-secrets` Secret. UAT and PROD should use the same application contract with an AWS-managed secret or an OIDC/JWK issuer; no token secret belongs in Git. The chart fails closed by default when an environment does not provide an explicit security override.

Expand Down
5 changes: 5 additions & 0 deletions helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ spec:
name: {{ .Values.security.secretName | quote }}
key: {{ .Values.security.secretKey | quote }}
{{- end }}
- name: REDIS_PASSWORD
valueFrom:
secretKeyRef:
name: {{ .Values.redis.secretName | quote }}
key: {{ .Values.redis.secretKey | quote }}
securityContext:
{{- toYaml .Values.containerSecurityContext | nindent 12 }}
ports:
Expand Down
4 changes: 4 additions & 0 deletions helm/values-sit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ runtime:

security:
enabled: true

redis:
secretName: redis
secretKey: REDIS_PASSWORD
4 changes: 4 additions & 0 deletions helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ security:
secretName: auth-service-secrets
secretKey: jwt-secret

redis:
secretName: redis
secretKey: REDIS_PASSWORD

service:
type: ClusterIP
port: 8080
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.digitalbank.apigateway.security;

import java.util.Base64;
import java.util.List;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
Expand All @@ -12,6 +13,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator;
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
import org.springframework.security.oauth2.jwt.JwtClaimValidator;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.jwt.JwtValidators;
import org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder;
Expand All @@ -30,21 +32,28 @@ class GatewayJwtDecoderConfiguration {
ReactiveJwtDecoder gatewayReactiveJwtDecoder(
@Value("${auth.jwt.issuer:}") String issuer,
@Value("${auth.jwt.secret:}") String secret,
@Value("${auth.jwt.jwk-set-uri:}") String jwkSetUri) {
@Value("${auth.jwt.jwk-set-uri:}") String jwkSetUri,
@Value("${auth.jwt.audience:}") String audience,
@Value("${auth.jwt.token-purpose:}") String tokenPurpose) {
if (!StringUtils.hasText(audience) || !StringUtils.hasText(tokenPurpose)) {
throw new IllegalStateException(
"Gateway security is enabled but auth.jwt.audience and auth.jwt.token-purpose are not configured");
}
if (StringUtils.hasText(secret)) {
return hmacDecoder(secret, issuer);
return hmacDecoder(secret, issuer, audience, tokenPurpose);
}
if (StringUtils.hasText(jwkSetUri)) {
return jwkDecoder(jwkSetUri, issuer);
return jwkDecoder(jwkSetUri, issuer, audience, tokenPurpose);
}
if (StringUtils.hasText(issuer)) {
return ReactiveJwtDecoders.fromIssuerLocation(issuer);
return configuredDecoder((NimbusReactiveJwtDecoder) ReactiveJwtDecoders.fromIssuerLocation(issuer), issuer,
audience, tokenPurpose);
}
throw new IllegalStateException(
"Gateway security is enabled but no auth.jwt.secret, auth.jwt.jwk-set-uri, or auth.jwt.issuer is configured");
}

private ReactiveJwtDecoder hmacDecoder(String encodedSecret, String issuer) {
private ReactiveJwtDecoder hmacDecoder(String encodedSecret, String issuer, String audience, String tokenPurpose) {
final byte[] secret;
try {
secret = Base64.getDecoder().decode(encodedSecret);
Expand All @@ -54,22 +63,30 @@ private ReactiveJwtDecoder hmacDecoder(String encodedSecret, String issuer) {
if (secret.length < MINIMUM_HMAC_KEY_BYTES) {
throw new IllegalStateException("auth.jwt.secret must decode to at least 32 bytes");
}
return configuredDecoder(new SecretKeySpec(secret, "HmacSHA256"), issuer);
return configuredDecoder(new SecretKeySpec(secret, "HmacSHA256"), issuer, audience, tokenPurpose);
}

private ReactiveJwtDecoder jwkDecoder(String jwkSetUri, String issuer) {
private ReactiveJwtDecoder jwkDecoder(String jwkSetUri, String issuer, String audience, String tokenPurpose) {
NimbusReactiveJwtDecoder decoder = NimbusReactiveJwtDecoder.withJwkSetUri(jwkSetUri).build();
if (StringUtils.hasText(issuer)) {
decoder.setJwtValidator(JwtValidators.createDefaultWithIssuer(issuer));
}
return decoder;
return configuredDecoder(decoder, issuer, audience, tokenPurpose);
}

private ReactiveJwtDecoder configuredDecoder(SecretKey key, String issuer) {
private ReactiveJwtDecoder configuredDecoder(SecretKey key, String issuer, String audience, String tokenPurpose) {
NimbusReactiveJwtDecoder decoder = NimbusReactiveJwtDecoder.withSecretKey(key).build();
return configuredDecoder(decoder, issuer, audience, tokenPurpose);
}

private ReactiveJwtDecoder configuredDecoder(
NimbusReactiveJwtDecoder decoder, String issuer, String audience, String tokenPurpose) {
OAuth2TokenValidator<Jwt> audienceValidator = new JwtClaimValidator<List<String>>(
"aud", values -> values != null && values.contains(audience));
OAuth2TokenValidator<Jwt> purposeValidator = new JwtClaimValidator<String>(
"token_purpose", tokenPurpose::equals);
if (StringUtils.hasText(issuer)) {
OAuth2TokenValidator<Jwt> issuerValidator = JwtValidators.createDefaultWithIssuer(issuer);
decoder.setJwtValidator(new DelegatingOAuth2TokenValidator<>(issuerValidator));
decoder.setJwtValidator(new DelegatingOAuth2TokenValidator<>(issuerValidator, audienceValidator, purposeValidator));
} else {
decoder.setJwtValidator(new DelegatingOAuth2TokenValidator<>(audienceValidator, purposeValidator));
}
return decoder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ SecurityWebFilterChain gatewaySecurityWebFilterChain(ServerHttpSecurity http,
.hasAuthority("SCOPE_transfer.internal")
.pathMatchers("/internal/v1/payment-instructions", "/internal/v1/payment-instructions/**")
.hasAuthority("SCOPE_payment.internal")
.pathMatchers("/api/v1/customers/**", "/api/v1/accounts/**")
.authenticated()
.pathMatchers("/api/v1/customers/*/accounts")
.hasAnyAuthority("SCOPE_account.self", "SCOPE_admin.internal")
.pathMatchers("/api/v1/customers/**")
.hasAnyAuthority("SCOPE_customer.self", "SCOPE_admin.internal")
.pathMatchers("/api/v1/accounts/**")
.hasAnyAuthority("SCOPE_account.self", "SCOPE_admin.internal")
.anyExchange().denyAll())
.oauth2ResourceServer(oauth2 -> oauth2
.jwt(withDefaults())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"spring.cloud.config.enabled=false",
"gateway.security.enabled=true",
"auth.jwt.issuer=https://issuer.test",
"auth.jwt.audience=api-gateway",
"auth.jwt.token-purpose=user-access",
"management.health.redis.enabled=false" })
@Import(GatewaySecurityIntegrationTests.TestJwtConfiguration.class)
class GatewaySecurityIntegrationTests {
Expand Down
Loading