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
14 changes: 13 additions & 1 deletion docs/constructs/cognito-user-pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Exactly one of `CognitoDomainPrefix` or (`DomainName` + `AuthSubDomain`) must be
| `AuthSubDomain` | `string?` | `null` | Subdomain for custom domain (e.g., `auth` → `auth.example.com`) |
| `ManagedLoginVersion` | `CognitoManagedLoginVersion` | `ManagedLogin` | `ClassicHostedUi` or `ManagedLogin` (v2) |
| `CreateRoute53Record` | `bool` | `true` | Whether to create an A-alias record in the hosted zone (custom domain only) |
| `Certificate` | `ICertificate?` | `null` | Existing ACM certificate (must be in `us-east-1`); when `null`, one is created with DNS validation (custom domain only) |
| `HostedZone` | `IHostedZone?` | `null` | Public hosted zone for `DomainName`; when supplied, no `HostedZone.FromLookup` context lookup is performed, so the stack synthesizes without AWS credentials (custom domain only) |

### App Client Properties (`CognitoUserPoolAppClientProps`)

Expand Down Expand Up @@ -148,7 +150,17 @@ var pool = new CognitoUserPoolConstruct(this, "auth-pool", new CognitoUserPoolCo
});
```

> The hosted zone for `DomainName` must already exist in the same account. The construct performs a `HostedZone.FromLookup` and creates an ACM certificate with DNS validation automatically.
> The hosted zone for `DomainName` must already exist in the same account. Unless `HostedZone` is supplied, the construct performs a `HostedZone.FromLookup` (a synth-time context lookup that needs AWS credentials or a cached `cdk.context.json` value). It creates an ACM certificate with DNS validation automatically unless `Certificate` is supplied.
>
> To synthesize without AWS credentials (for example, in CI), pass a zone you already know:
>
> ```csharp
> HostedZone = HostedZone.FromHostedZoneAttributes(this, "zone", new HostedZoneAttributes
> {
> HostedZoneId = "Z0123456789EXAMPLE",
> ZoneName = "example.com",
> }),
> ```

### Resource Servers and API Scopes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ private void ConfigureUserPoolDomain(string id, ICognitoUserPoolConstructProps p
}
else
{
var zone = HostedZone.FromLookup(this, $"{id}-hosted-zone", new HostedZoneProviderProps
var zone = props.Domain.HostedZone ?? HostedZone.FromLookup(this, $"{id}-hosted-zone", new HostedZoneProviderProps
{
DomainName = props.Domain.DomainName!,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Amazon.CDK.AWS.CertificateManager;
using Amazon.CDK.AWS.Route53;

namespace LayeredCraft.Cdk.Constructs.Models;

Expand All @@ -23,6 +24,14 @@ public interface ICognitoUserPoolDomainProps
/// in the stack's region (valid when the stack is already deployed to <c>us-east-1</c>).
/// </summary>
ICertificate? Certificate { get; }

/// <summary>
/// The public hosted zone for <see cref="DomainName"/>, used to validate a created certificate and
/// to create the alias record. When provided, the construct performs no hosted-zone context lookup,
/// so the stack synthesizes without AWS credentials. If <see langword="null"/>, the zone is found
/// with <c>HostedZone.FromLookup</c>.
/// </summary>
IHostedZone? HostedZone => null;
}

public sealed record CognitoUserPoolDomainProps : ICognitoUserPoolDomainProps
Expand All @@ -40,6 +49,9 @@ public sealed record CognitoUserPoolDomainProps : ICognitoUserPoolDomainProps

/// <inheritdoc />
public ICertificate? Certificate { get; init; }

/// <inheritdoc />
public IHostedZone? HostedZone { get; init; }
}

public enum CognitoManagedLoginVersion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Amazon.CDK;
using Amazon.CDK.Assertions;
using Amazon.CDK.AWS.Route53;
using AwesomeAssertions;
using LayeredCraft.Cdk.Constructs;
using LayeredCraft.Cdk.Constructs.Models;
Expand Down Expand Up @@ -81,6 +82,47 @@ public void Construct_ShouldNotCreateDomain_WhenDomainIsNull()
template.ResourceCountIs("AWS::Cognito::UserPoolDomain", 0);
}

[Fact]
public void Construct_ShouldUseSuppliedHostedZone_WithoutContextLookup_WhenCustomDomainHasHostedZone()
{
var app = new App();
var stack = CreateEnvironmentStack(app);
var zone = HostedZone.FromHostedZoneAttributes(stack, "zone", new HostedZoneAttributes
{
HostedZoneId = "Z0123456789EXAMPLE",
ZoneName = "example.com",
});

_ = new CognitoUserPoolConstruct(stack, "test-pool", CustomDomainProps(new CognitoUserPoolDomainProps
{
DomainName = "example.com",
AuthSubDomain = "auth",
HostedZone = zone,
}));

app.Synth().Manifest.Missing.Should().BeNullOrEmpty();
Template.FromStack(stack).HasResourceProperties("AWS::Route53::RecordSet", new Dictionary<string, object>
{
["Name"] = "auth.example.com.",
["HostedZoneId"] = "Z0123456789EXAMPLE",
});
}

[Fact]
public void Construct_ShouldLookUpHostedZone_WhenCustomDomainHasNoHostedZone()
{
var app = new App();
var stack = CreateEnvironmentStack(app);

_ = new CognitoUserPoolConstruct(stack, "test-pool", CustomDomainProps(new CognitoUserPoolDomainProps
{
DomainName = "example.com",
AuthSubDomain = "auth",
}));

app.Synth().Manifest.Missing.Should().ContainSingle().Which.Key.Should().StartWith("hosted-zone:");
}

// -------------------------------------------------------------------------
// App client
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -270,4 +312,19 @@ public void Construct_ShouldExportAppClientId(CognitoUserPoolConstructProps prop
// -------------------------------------------------------------------------

private static Stack CreateStack() => new(new App(), "test-stack");

// Context lookups require an explicit account and region.
private static Stack CreateEnvironmentStack(App app) => new(app, "test-stack", new StackProps
{
Env = new Amazon.CDK.Environment { Account = "123456789012", Region = "us-east-1" },
});

private static CognitoUserPoolConstructProps CustomDomainProps(CognitoUserPoolDomainProps domain)
{
var props = CdkTestHelper.CreateCognitoUserPoolPropsBuilder()
.AddWebAppClient("client", ["https://example.com/callback"], ["https://example.com"])
.Build();

return props with { Domain = domain };
}
}
Loading