diff --git a/docs/adr/email-outbound-relay.md b/docs/adr/email-outbound-relay.md index 614a90e25f..41683adb47 100644 --- a/docs/adr/email-outbound-relay.md +++ b/docs/adr/email-outbound-relay.md @@ -78,6 +78,37 @@ are unchanged. and that replies thread against the sender's Sent copy despite SES's Message-ID rewrite. If threading fails, report it before switching relay. +## The business site's sender + +The business site sends its own automated mail, such as sign-in codes and +invitations, straight through SES rather than through company Email +(#5400). It needs no mailbox, so it does not wait on the Email server's +sign-in, recovery or public submission work, and it cannot reach any mailbox. + +- **A second send-only credential.** A separate IAM user may only + `ses:SendRawEmail` on the same domain identity, with the sender equal to + `no-reply@devantler.tech`. Its access key, policy and attachment are + platform-owned like the relay's, and no policy includes Delete. +- **Custody.** A platform PushSecret copies the key ID and the derived SMTP + password to `apps/business-site/smtp` in OpenBao, as `username` and + `password`. The business site reads that one key through its dedicated role, + which also reads its public disclosure and nothing else. +- **Use.** The site's mail client connects to + `email-smtp.eu-central-1.amazonaws.com` on port 587 with STARTTLS and SMTP + authentication, sending from `no-reply@devantler.tech`. The tenant owns its + own egress to that endpoint. +- **Rotation.** Declare a new access key; the PushSecret then writes the new + credential. Remove the old key afterwards, with Delete added to its + policies first. +- **Revocation.** No policy includes Delete, so removing a manifest leaves the + AWS object in place and the credential still sends. To stop sending, first + add Delete to the attachment's management policies and let it reconcile, + then remove the attachment and check in AWS that the user has no policy + attached. + +While the account is in the SES sandbox, this sender too reaches only verified +recipients. + ## Alternatives - **Direct delivery:** needs a dedicated address, reverse DNS and egress diff --git a/k8s/bases/infrastructure/vault-config/job.yaml b/k8s/bases/infrastructure/vault-config/job.yaml index dfb403d15d..24b8614764 100644 --- a/k8s/bases/infrastructure/vault-config/job.yaml +++ b/k8s/bases/infrastructure/vault-config/job.yaml @@ -836,14 +836,19 @@ spec: } POLICY - # Public staging reads one owner-maintained entry. It has no - # value writer, metadata listing or shared infrastructure policy. + # The business site reads two exact entries: its owner-maintained + # public disclosure, and its send-only SES SMTP credential, which a + # platform PushSecret writes (platform#5400). It has no value + # writer, metadata listing or shared infrastructure policy. bao policy write app-business-site-disclosure-readonly - <<'BUSINESS_SITE_POLICY' { "path": { "secret/data/apps/business-site/public-disclosure": { "capabilities": ["read"] }, + "secret/data/apps/business-site/smtp": { + "capabilities": ["read"] + }, "auth/token/lookup-self": { "capabilities": ["read"] }, @@ -1086,6 +1091,11 @@ spec: path "secret/data/infrastructure/email/ses-smtp" { capabilities = ["create", "update", "read"] } + # The business site's SES SMTP credential, pushed the same way + # into its own prefix (platform#5400). Exact key only. + path "secret/data/apps/business-site/smtp" { + capabilities = ["create", "update", "read"] + } path "secret/data/apps/actual-budget/*" { capabilities = ["create", "update", "read"] } @@ -1143,6 +1153,9 @@ spec: path "secret/metadata/infrastructure/email/ses-smtp" { capabilities = ["create", "update", "read"] } + path "secret/metadata/apps/business-site/smtp" { + capabilities = ["create", "update", "read"] + } path "secret/metadata/apps/actual-budget/*" { capabilities = ["create", "update", "read"] } diff --git a/k8s/providers/hetzner/apps/aws/access-key-business-site-smtp.yaml b/k8s/providers/hetzner/apps/aws/access-key-business-site-smtp.yaml new file mode 100644 index 0000000000..cb20cf7af4 --- /dev/null +++ b/k8s/providers/hetzner/apps/aws/access-key-business-site-smtp.yaml @@ -0,0 +1,29 @@ +# The business site's SES SMTP credential. The provider publishes the key ID as +# `username` and the SES SMTP password derived for its own region +# (eu-central-1) as `attribute.ses_smtp_password_v4`. The connection Secret +# stays in this namespace; push-secret-business-site-smtp.yaml copies those two +# values to the business site's OpenBao prefix, and nobody reads them. The +# password is only available when the key is created, so the OpenBao copy is +# the durable one. Rotate by declaring a new key, then remove this one with +# Delete added to its policies. +apiVersion: iam.aws.m.upbound.io/v1beta1 +kind: AccessKey +metadata: + name: business-site-smtp + namespace: aws + labels: + app.kubernetes.io/managed-by: ksail +spec: + managementPolicies: + - Observe + - Create + - Update + - LateInitialize + forProvider: + userRef: + name: business-site-smtp + writeConnectionSecretToRef: + name: business-site-smtp-credentials + providerConfigRef: + kind: ProviderConfig + name: default diff --git a/k8s/providers/hetzner/apps/aws/kustomization.yaml b/k8s/providers/hetzner/apps/aws/kustomization.yaml index a186b8dcdd..45cd4af103 100644 --- a/k8s/providers/hetzner/apps/aws/kustomization.yaml +++ b/k8s/providers/hetzner/apps/aws/kustomization.yaml @@ -16,6 +16,11 @@ resources: - user-policy-attachment-email-smtp.yaml - access-key-email-smtp.yaml - push-secret-email-smtp.yaml + - user-business-site-smtp.yaml + - policy-business-site-smtp-send.yaml + - user-policy-attachment-business-site-smtp.yaml + - access-key-business-site-smtp.yaml + - push-secret-business-site-smtp.yaml - role-ses-account-setup.yaml - dns-endpoint.yaml - external-secret.yaml diff --git a/k8s/providers/hetzner/apps/aws/policy-business-site-smtp-send.yaml b/k8s/providers/hetzner/apps/aws/policy-business-site-smtp-send.yaml new file mode 100644 index 0000000000..1fee0efb61 --- /dev/null +++ b/k8s/providers/hetzner/apps/aws/policy-business-site-smtp-send.yaml @@ -0,0 +1,41 @@ +# The only permission of the business site's SMTP identity: send raw mail +# through the company mail domain's SES identity in eu-central-1, from its one +# sender address. It cannot use any other sender, read, configure or verify +# anything in SES. +apiVersion: iam.aws.m.upbound.io/v1beta1 +kind: Policy +metadata: + name: business-site-smtp-send + namespace: aws + labels: + app.kubernetes.io/managed-by: ksail +spec: + managementPolicies: + - Observe + - Create + - Update + - LateInitialize + forProvider: + description: >- + Lets the business site's SMTP identity send mail through the company mail + domain's SES identity, from its one sender address only. + policy: | + { + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "SendBusinessSiteMail", + "Effect": "Allow", + "Action": "ses:SendRawEmail", + "Resource": "arn:aws:ses:eu-central-1:939001610192:identity/devantler.tech", + "Condition": { + "StringEquals": { + "ses:FromAddress": "no-reply@devantler.tech" + } + } + } + ] + } + providerConfigRef: + kind: ProviderConfig + name: default diff --git a/k8s/providers/hetzner/apps/aws/push-secret-business-site-smtp.yaml b/k8s/providers/hetzner/apps/aws/push-secret-business-site-smtp.yaml new file mode 100644 index 0000000000..7384706f94 --- /dev/null +++ b/k8s/providers/hetzner/apps/aws/push-secret-business-site-smtp.yaml @@ -0,0 +1,33 @@ +# Copies the business site's SES SMTP credential into OpenBao under the +# business site's own prefix, where its tenant reads it through its dedicated +# role. Platform-applied, so it may use the shared store, which holds an +# exact-key grant for this one record. Replace keeps OpenBao equal to the +# current key; a missing or incomplete connection Secret writes nothing. +apiVersion: external-secrets.io/v1alpha1 +kind: PushSecret +metadata: + name: business-site-smtp-credentials + namespace: aws + labels: + app.kubernetes.io/managed-by: ksail +spec: + updatePolicy: Replace + deletionPolicy: None + refreshInterval: 1h + secretStoreRefs: + - name: openbao + kind: ClusterSecretStore + selector: + secret: + name: business-site-smtp-credentials + data: + - match: + secretKey: username + remoteRef: + remoteKey: apps/business-site/smtp + property: username + - match: + secretKey: attribute.ses_smtp_password_v4 + remoteRef: + remoteKey: apps/business-site/smtp + property: password diff --git a/k8s/providers/hetzner/apps/aws/user-business-site-smtp.yaml b/k8s/providers/hetzner/apps/aws/user-business-site-smtp.yaml new file mode 100644 index 0000000000..f3a3c6138d --- /dev/null +++ b/k8s/providers/hetzner/apps/aws/user-business-site-smtp.yaml @@ -0,0 +1,23 @@ +# The business site's send-only SMTP identity for Amazon SES (platform#5400). +# It sends the site's automated mail, such as sign-in codes and invitations, +# apart from company Email's own relay identity. Platform-owned like that +# identity, so the public aws tenant cannot widen what it may do. Delete is +# omitted so a prune cannot remove it; revoke by replacing its access key. +apiVersion: iam.aws.m.upbound.io/v1beta1 +kind: User +metadata: + name: business-site-smtp + namespace: aws + labels: + app.kubernetes.io/managed-by: ksail +spec: + managementPolicies: + - Observe + - Create + - Update + - LateInitialize + forProvider: + path: /business-site/ + providerConfigRef: + kind: ProviderConfig + name: default diff --git a/k8s/providers/hetzner/apps/aws/user-policy-attachment-business-site-smtp.yaml b/k8s/providers/hetzner/apps/aws/user-policy-attachment-business-site-smtp.yaml new file mode 100644 index 0000000000..5e6a3018d9 --- /dev/null +++ b/k8s/providers/hetzner/apps/aws/user-policy-attachment-business-site-smtp.yaml @@ -0,0 +1,22 @@ +# Attaches the send-only policy to the business site's SMTP identity. +apiVersion: iam.aws.m.upbound.io/v1beta1 +kind: UserPolicyAttachment +metadata: + name: business-site-smtp-send + namespace: aws + labels: + app.kubernetes.io/managed-by: ksail +spec: + managementPolicies: + - Observe + - Create + - Update + - LateInitialize + forProvider: + userRef: + name: business-site-smtp + policyArnRef: + name: business-site-smtp-send + providerConfigRef: + kind: ProviderConfig + name: default diff --git a/scripts/check-business-disclosure-reader/main.go b/scripts/check-business-disclosure-reader/main.go index 8b6deebe90..9e3f3de1bb 100644 --- a/scripts/check-business-disclosure-reader/main.go +++ b/scripts/check-business-disclosure-reader/main.go @@ -23,6 +23,7 @@ func expectedPayload(kind string) string { case "policy": return `{"path":{ "secret/data/apps/business-site/public-disclosure":{"capabilities":["read"]}, +"secret/data/apps/business-site/smtp":{"capabilities":["read"]}, "auth/token/lookup-self":{"capabilities":["read"]}, "auth/token/revoke-self":{"capabilities":["update"]}}}` default: diff --git a/scripts/tests/arc-staging/business_site_sender_test.go b/scripts/tests/arc-staging/business_site_sender_test.go new file mode 100644 index 0000000000..663ea094e8 --- /dev/null +++ b/scripts/tests/arc-staging/business_site_sender_test.go @@ -0,0 +1,144 @@ +package arcstaging_test + +import ( + "encoding/json" + "errors" + "reflect" + "regexp" + "slices" + "strings" + "testing" +) + +// businessSiteSeedGrants refuses any seed grant that can reach the business +// site's prefix other than the two exact SMTP credential grants, including a +// glob (`*`) or segment wildcard (`+`) that covers it. +func businessSiteSeedGrants(policy string) error { + stanzas := regexp.MustCompile(`(?s)path\s+"([^"]*)"\s*\{\s*capabilities\s*=\s*(\[[^\]]+\])\s*\}`).FindAllStringSubmatch(policy, -1) + if len(stanzas) == 0 || len(stanzas) != strings.Count(policy, "path \"") { + return errors.New("every seed policy path must parse as one capabilities stanza") + } + probes := []string{ + "secret/data/apps/business-site/smtp", + "secret/data/apps/business-site/other", + "secret/metadata/apps/business-site/smtp", + "secret/metadata/apps/business-site/other", + } + want := map[string]bool{ + "secret/data/apps/business-site/smtp": true, + "secret/metadata/apps/business-site/smtp": true, + } + for _, stanza := range stanzas { + pattern := regexp.QuoteMeta(stanza[1]) + pattern = strings.ReplaceAll(pattern, `\+`, `[^/]+`) + if strings.HasSuffix(pattern, `\*`) { + pattern = strings.TrimSuffix(pattern, `\*`) + `.*` + } + matcher := regexp.MustCompile(`^` + pattern + `$`) + if !slices.ContainsFunc(probes, matcher.MatchString) { + continue + } + if !want[stanza[1]] { + return errors.New("the business site's seed authorization is duplicated or broader than its SMTP credential: " + stanza[1]) + } + delete(want, stanza[1]) + var capabilities []string + if err := json.Unmarshal([]byte(stanza[2]), &capabilities); err != nil { + return err + } + slices.Sort(capabilities) + if !reflect.DeepEqual(capabilities, []string{"create", "read", "update"}) { + return errors.New("the SMTP credential grant must write and read without list or delete authority") + } + } + if len(want) != 0 { + return errors.New("need exactly one data and one metadata grant for the business site's SMTP credential") + } + return nil +} + +// The business site's send-only SES credential (platform#5400) reaches +// OpenBao only through its platform PushSecret, at one exact key under the +// business site's own prefix, and may send only from its one sender address. +func TestBusinessSiteSenderCredentialIsExactAndSendOnly(t *testing.T) { + _, seed := captureBaoWrite(t, "bao", "policy", "write", "vault-seed-write") + if err := businessSiteSeedGrants(seed); err != nil { + t.Fatal(err) + } + const exact = `path "secret/data/apps/business-site/smtp" { + capabilities = ["create", "update", "read"] + }` + for name, widened := range map[string]string{ + "apps glob": `path "secret/data/apps/*" { capabilities = ["read"] }`, + "secret glob": `path "secret/*" { capabilities = ["read"] }`, + "prefix glob": `path "secret/data/apps/business-site*" { capabilities = ["read"] }`, + "segment wildcard": `path "secret/metadata/apps/+/smtp" { capabilities = ["read"] }`, + "other key": `path "secret/data/apps/business-site/other" { capabilities = ["read"] }`, + "duplicate": exact, + } { + if businessSiteSeedGrants(seed+"\n"+widened+"\n") == nil { + t.Fatalf("a %s grant over the business site's prefix was accepted", name) + } + } + withDelete := regexp.MustCompile(`(path\s+"secret/data/apps/business-site/smtp"\s*\{\s*capabilities\s*=\s*\[)`).ReplaceAllString(seed, `${1}"delete", `) + if withDelete == seed { + t.Fatal("the delete control did not change the seed policy") + } + if businessSiteSeedGrants(withDelete) == nil { + t.Fatal("a delete capability on the business site's SMTP credential was accepted") + } + + push := readYAML(t, "k8s/providers/hetzner/apps/aws/push-secret-business-site-smtp.yaml") + equal(t, mailField[string](t, push, "metadata", "namespace"), "aws") + equal(t, mailField[string](t, push, "spec", "selector", "secret", "name"), "business-site-smtp-credentials") + equal(t, mailField[string](t, push, "spec", "updatePolicy"), "Replace") + equal(t, mailField[string](t, push, "spec", "deletionPolicy"), "None") + data := mailField[[]any](t, push, "spec", "data") + equal(t, len(data), 2) + pushed := map[string]string{} + for _, entry := range data { + equal(t, mailField[string](t, entry, "match", "remoteRef", "remoteKey"), "apps/business-site/smtp") + pushed[mailField[string](t, entry, "match", "secretKey")] = mailField[string](t, entry, "match", "remoteRef", "property") + } + equal(t, pushed["username"], "username") + equal(t, pushed["attribute.ses_smtp_password_v4"], "password") + + key := readYAML(t, "k8s/providers/hetzner/apps/aws/access-key-business-site-smtp.yaml") + equal(t, mailField[string](t, key, "spec", "forProvider", "userRef", "name"), "business-site-smtp") + equal(t, mailField[string](t, key, "spec", "writeConnectionSecretToRef", "name"), "business-site-smtp-credentials") + attachment := readYAML(t, "k8s/providers/hetzner/apps/aws/user-policy-attachment-business-site-smtp.yaml") + equal(t, mailField[string](t, attachment, "spec", "forProvider", "userRef", "name"), "business-site-smtp") + equal(t, mailField[string](t, attachment, "spec", "forProvider", "policyArnRef", "name"), "business-site-smtp-send") + for _, path := range []string{ + "user-business-site-smtp.yaml", + "policy-business-site-smtp-send.yaml", + "user-policy-attachment-business-site-smtp.yaml", + "access-key-business-site-smtp.yaml", + } { + policies := mailField[[]any](t, readYAML(t, "k8s/providers/hetzner/apps/aws/"+path), "spec", "managementPolicies") + if slices.Contains(policies, any("Delete")) || slices.Contains(policies, any("*")) { + t.Fatalf("%s must not let a prune delete the sender identity", path) + } + } + + var document struct { + Version string + Statement []map[string]any + } + source := mailField[string](t, readYAML(t, "k8s/providers/hetzner/apps/aws/policy-business-site-smtp-send.yaml"), "spec", "forProvider", "policy") + if err := json.Unmarshal([]byte(source), &document); err != nil { + t.Fatal(err) + } + wantStatement := map[string]any{ + "Sid": "SendBusinessSiteMail", + "Effect": "Allow", + "Action": "ses:SendRawEmail", + "Resource": "arn:aws:ses:eu-central-1:939001610192:identity/devantler.tech", + "Condition": map[string]any{ + "StringEquals": map[string]any{"ses:FromAddress": "no-reply@devantler.tech"}, + }, + } + if document.Version != "2012-10-17" || len(document.Statement) != 1 || !reflect.DeepEqual(document.Statement[0], wantStatement) { + t.Fatal("the business site's SMTP identity may only send raw mail from its one sender address through the company mail domain") + } +} diff --git a/scripts/tests/test-business-disclosure-reader.sh b/scripts/tests/test-business-disclosure-reader.sh index 24d91313df..f6a51df306 100755 --- a/scripts/tests/test-business-disclosure-reader.sh +++ b/scripts/tests/test-business-disclosure-reader.sh @@ -48,7 +48,7 @@ valid_policy() { } valid_role "${role}" || fail 'role must bind one exact identity and one read-only policy with a bounded lifetime' -valid_policy "${policy}" || fail 'policy must read only the selected disclosure entry' +valid_policy "${policy}" || fail 'policy must read only the selected disclosure and SMTP credential entries' for kind in role policy; do if [ "${kind}" = role ]; then value="${role}"; else value="${policy}"; fi @@ -82,6 +82,8 @@ for change in \ '.path["secret/data/apps/business-site/public-disclosure"].capabilities += ["update"]' \ '.path = {"secret/data/apps/another-app/public-disclosure": {"capabilities": ["read"]}}' \ '.path["secret/data/apps/business-site/other"] = {"capabilities": ["read"]}' \ + '.path["secret/data/apps/business-site/smtp"].capabilities += ["update"]' \ + '.path["secret/metadata/apps/business-site/smtp"] = {"capabilities": ["read", "list"]}' \ '.path["auth/token/*"] = {"capabilities": ["read", "update"]}' \ '.path["auth/token/lookup-self"].capabilities += ["update"]' \ 'del(.path["auth/token/revoke-self"])'; do @@ -94,4 +96,4 @@ grep -Fq "'scripts/tests/test-business-disclosure-reader.sh'" "${root_dir}/.gith grep -Fq 'bash scripts/tests/test-business-disclosure-reader.sh' "${root_dir}/.github/workflows/ci.yaml" || fail 'CI must execute this contract' -printf 'PASS: rendered business disclosure identity and 22 invalid-payload controls\n' +printf 'PASS: rendered business disclosure identity and 24 invalid-payload controls\n' diff --git a/scripts/validate-eks-ci-role-policy/approved-surface.txt b/scripts/validate-eks-ci-role-policy/approved-surface.txt index 81fe16a525..e6342317fd 100644 --- a/scripts/validate-eks-ci-role-policy/approved-surface.txt +++ b/scripts/validate-eks-ci-role-policy/approved-surface.txt @@ -128,8 +128,12 @@ helm.toolkit.fluxcd.io/v2|HelmRelease|vertical-pod-autoscaler|vertical-pod-autos helm.toolkit.fluxcd.io/v2|HelmRelease|whoami|whoami 22eb185e9f69bbddf8dc27bd2a852b07b3bbaf791d25ada894eeb8e954f99bed +iam.aws.m.upbound.io/v1beta1|AccessKey|aws|business-site-smtp bd2f3c05c077842bef856173ff21446e9de19ba48143abe869a5ed146aa071e0 + iam.aws.m.upbound.io/v1beta1|AccessKey|aws|email-smtp 22ff8010a4298f2b19a2829a65c383f27fd737b2aed7b096f22814ad73a2f240 +iam.aws.m.upbound.io/v1beta1|Policy|aws|business-site-smtp-send c18118edf62d1b5536b9d5b72c4e5cbcff8a03b3e79dc65cf1a3c3160e548c8d + iam.aws.m.upbound.io/v1beta1|Policy|aws|eks-ci-smoke-boundary 019210611429956763cab81a883bc6cf1faf53caf89b5e9274e8ec69cad01b66 iam.aws.m.upbound.io/v1beta1|Policy|aws|email-smtp-send 38284030cdef086fa0a51443feba839b2f6fcf27bf1cc177fa2fa15d314ceb30 @@ -138,8 +142,12 @@ iam.aws.m.upbound.io/v1beta1|Role|aws|eks-ci c40205b3f48d193e8be1a39abda738865b9 iam.aws.m.upbound.io/v1beta1|Role|aws|ses-account-setup 7faa58457f4a17a3c14368d3a956bb07da4f40bb2689c61149ee31eae9693b6b +iam.aws.m.upbound.io/v1beta1|UserPolicyAttachment|aws|business-site-smtp-send f1ac1f07ba8064b97981e45afa17f959b4c133d5648ebd6892e4756db41aa2b1 + iam.aws.m.upbound.io/v1beta1|UserPolicyAttachment|aws|email-smtp-send 06c8f5c0aa8f7b593878b9869c5af7375a1ef2f429182cb904e76e0dae966e1c +iam.aws.m.upbound.io/v1beta1|User|aws|business-site-smtp 1717b61eedee267eff58840a625747e71514878135154c324f3cf63051fd4847 + iam.aws.m.upbound.io/v1beta1|User|aws|email-smtp 19ce46105be088b444658d4d594f6059308f079440336af06af912d60aeb5704 kro.run/v1alpha1|ResourceGraphDefinition||tenant.kro.run 387506d9d05e1b17191d74e4fb3b45ea3292a09484a082272443fc12e0d51745