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
29 changes: 18 additions & 11 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
# A tag v<semver> is a release: GoReleaser builds every platform Terraform runs on, and signs the
# checksums with the GPG key registry.terraform.io holds for spin-stack, which is how the registry
# and every `terraform init` know the binaries are ours.
# A tag v<semver> is a release: GoReleaser builds every platform Terraform and OpenTofu run on, and
# signs the checksums with the key the registries hold for spin-stack - which is how every
# `init` knows the binaries are ours.
#
# The key is the repository's GPG_PRIVATE_KEY and PASSPHRASE secrets, and its public half is in the
# registry's signing keys for spin-stack. Neither is set until a person publishes the provider.
# The key is in KMS (spin-stack/ci's signing.tf) and never leaves it: this job assumes the one role
# that may sign with it, which only the release environment's jobs may assume - and GitHub lets
# only a v* tag deploy to that environment. Its public half, for the registries, is
# `go run ./internal/cmd/kmspgp -key alias/terraform-provider-spin-signing public`.
name: Release

on:
push:
tags: ['v*']

permissions:
contents: write
contents: read

jobs:
release:
runs-on: ubuntu-latest
timeout-minutes: 30
environment: release
permissions:
# The GitHub release and its files.
contents: write
# The OIDC token the signing role is assumed with.
id-token: write
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
Expand All @@ -25,17 +33,16 @@ jobs:
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version-file: go.mod
# This job signs: its Go comes from no cache another job saved.
cache: false

- id: gpg
uses: crazy-max/ghaction-import-gpg@2dc316deee8e90f13e1a351ab510b4d5bc0c82cd # v7.0.0
- uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0
with:
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.PASSPHRASE }}
role-to-assume: arn:aws:iam::277203350136:role/terraform-provider-spin-release
aws-region: us-west-2

- uses: goreleaser/goreleaser-action@a4f02ab5355a589de60d917a659ce617ed19b3de # v7.3.0
with:
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GPG_FINGERPRINT: ${{ steps.gpg.outputs.fingerprint }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/_output/
/terraform-provider-spin
/dist/
9 changes: 6 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# What registry.terraform.io takes for a provider: a zip per platform named
# What registry.terraform.io and registry.opentofu.org take for a provider: a zip per platform named
# terraform-provider-spin_<version>_<os>_<arch>.zip, the manifest, and SHA256SUMS signed by the key
# it holds for spin-stack.
# they hold for spin-stack.
version: 2
before:
hooks:
Expand Down Expand Up @@ -28,8 +28,11 @@ checksum:
name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS'
algorithm: sha256
signs:
# By the KMS key spin-stack/ci's signing.tf keeps, as the role this run assumed: kmspgp wraps what
# KMS signs in OpenPGP, as gpg --detach-sign would write it.
- artifacts: checksum
args: ["--batch", "--local-user", "{{ .Env.GPG_FINGERPRINT }}", "--output", "${signature}", "--detach-sign", "${artifact}"]
cmd: go
args: ["run", "./internal/cmd/kmspgp", "-key", "alias/terraform-provider-spin-signing", "sign", "-out", "${signature}", "${artifact}"]
release:
extra_files:
- glob: terraform-registry-manifest.json
Expand Down
17 changes: 16 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,32 @@ go 1.27.0
toolchain go1.27.4

require (
github.com/ProtonMail/go-crypto v1.5.2
github.com/aws/aws-sdk-go-v2 v1.48.0
github.com/aws/aws-sdk-go-v2/config v1.33.19
github.com/aws/aws-sdk-go-v2/service/kms v1.63.0
github.com/hashicorp/terraform-plugin-framework v1.19.0
github.com/hashicorp/terraform-plugin-go v0.31.0
github.com/hashicorp/terraform-plugin-testing v1.16.0
github.com/spin-stack/go-tools v0.0.0-20261117155930-fbf9e387ffaa
)

require (
github.com/ProtonMail/go-crypto v1.4.1 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.21.1 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.12 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.25 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.12 // indirect
github.com/aws/aws-sdk-go-v2/service/signin v1.13.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.39.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.52.0 // indirect
github.com/aws/smithy-go v1.28.9 // indirect
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudflare/circl v1.6.3 // indirect
Expand Down
64 changes: 32 additions & 32 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,45 @@ github.com/AzureAD/microsoft-authentication-library-for-go v1.8.0 h1:Nljr4q1GRA/
github.com/AzureAD/microsoft-authentication-library-for-go v1.8.0/go.mod h1:Y33QHnf0FfdVewFFISOGe20mkZbxX4H839o955/PoeI=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/ProtonMail/go-crypto v1.4.1 h1:9RfcZHqEQUvP8RzecWEUafnZVtEvrBVL9BiF67IQOfM=
github.com/ProtonMail/go-crypto v1.4.1/go.mod h1:e1OaTyu5SYVrO9gKOEhTc+5UcXtTUa+P3uLudwcgPqo=
github.com/ProtonMail/go-crypto v1.5.2 h1:cucYnvqcY7UOXVD//mSyjeaPY0SSN3v5cDkYPxumINk=
github.com/ProtonMail/go-crypto v1.5.2/go.mod h1:/RaSu30DaKO4RY+XdV/ACcCcZkGr7AhUIduq5sjzzCo=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/aws/aws-sdk-go-v2 v1.47.3 h1:HRoT/MkNi3bjO5xF4dCzLzvVmkvRGHj3/cLINDnJejk=
github.com/aws/aws-sdk-go-v2 v1.47.3/go.mod h1:xSl+OSI5ZUw6bSMupWc218zE5Pl+65MCCu3ySWFY99A=
github.com/aws/aws-sdk-go-v2/config v1.33.9 h1:Hv6bcQUJVnmuWaKjqD6Ot8sE75NrWsWV20oGT5aWle0=
github.com/aws/aws-sdk-go-v2/config v1.33.9/go.mod h1:ivowmrZNfBQLcg4QkaItgi5wae4Bzot9RzIdbY1hfTg=
github.com/aws/aws-sdk-go-v2/credentials v1.20.9 h1:SNO55PnWhaDafsOcv16lCA0kdoFMl/YlegWoVsU02s0=
github.com/aws/aws-sdk-go-v2/credentials v1.20.9/go.mod h1:+2reOTRtdD3VSBqpRDNwrZIYy8yMCYa7oK3WKTJ/I1I=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.3 h1:gr3b87OLB29jymWPzp/CAyXwQRJVE+sC4Wm0qBIKHsk=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.3/go.mod h1:BRjf4L/Yx2hY54o+ZibPbLJyMMeSwqYQXwcJfqW66a8=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.6 h1:CqL8NN3p4I0JTwwV5MEoatIl4Y2amZ2X9dRD+0hbRL0=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.6/go.mod h1:exdJ32hMqXl0Wyo6NwqEC5yIhtua3+PDaKE8P29m4qE=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.6 h1:FpBq24TG6WVSObTNhQOv7Q0088rounUUlwn3zlNItkM=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.6/go.mod h1:ONzdPVYC95oBOu0VWNEsKEntcoemP3xVKjT+qWml1s4=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.6 h1:Qmn60RWuk9+bd2ddvJJqwf3Cpac3VvGMOdbGemtUhX4=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.6/go.mod h1:h/YJhe4AVyQxEUMluph10iqV7mjajH7A9L+BHF5ppis=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.21 h1:e6YW5uME30TkrElR+4/xcY7NB6CCj2dcv7vtmGXhfhQ=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.21/go.mod h1:obpHAMP693+f3/A8PkLhIqfK1C+NWWn8zXzqD3OH520=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.6 h1:LHHdobjo5le1sjJdolitV7uVvYg3wkaeM1Ju2fDgeZQ=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.6/go.mod h1:CBGB0evAiaMD3Lk4wPO7LQCcCZLuhzL5Qf/wfEUHQ5w=
github.com/aws/aws-sdk-go-v2/service/kms v1.61.4 h1:vW7497x7W6ucLJAqDt96jIdO4+IWLMB3p98UubwjdqM=
github.com/aws/aws-sdk-go-v2/service/kms v1.61.4/go.mod h1:Vz59N7x42VGLXZ5dGL9b10QhrU4GQv7MqnO7wc8bl9U=
github.com/aws/aws-sdk-go-v2/service/signin v1.10.4 h1:tvpDjuUvgKURf3Yk4hOaGbZ4QdY+070OWcA5d6LUv2A=
github.com/aws/aws-sdk-go-v2/service/signin v1.10.4/go.mod h1:3ibVKg5Ng82Zby8++jELTD2nHxiPk2DJRLEeW/LKp8M=
github.com/aws/aws-sdk-go-v2/service/sso v1.38.4 h1:ChCtagixSFOxltv0o6E3NT8F7ksvZtZGPwUBynO5Nmk=
github.com/aws/aws-sdk-go-v2/service/sso v1.38.4/go.mod h1:+FEO9jrVMpc9oN3CdAxJbtlQEu3z8UddDPSedpeM3PY=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.43.4 h1:j6c+HA4AoS0EBEjArPjavFEBrucJEryfrQU0TffmACk=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.43.4/go.mod h1:RhghaXUk9q66VtOQUX1FdPddQuHnqQZzbB0fEYXmPRA=
github.com/aws/aws-sdk-go-v2/service/sts v1.51.4 h1:OKEfbf2tcfsF2pBk/S9QqNpo6alaAtiBs4hysCsBjPw=
github.com/aws/aws-sdk-go-v2/service/sts v1.51.4/go.mod h1:mCd6qGo9b8hTtfjgsmNkFlzwjyce4owd7HDVFnCLXhM=
github.com/aws/smithy-go v1.28.5 h1:OqbqKtzY9NUnr2yAe9+hiTdFb20tNaK/lI0PMOGXX9k=
github.com/aws/smithy-go v1.28.5/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
github.com/aws/aws-sdk-go-v2 v1.48.0 h1:tFU7EJVm7MfyaXLsXdYTZ0Vd6vwt9OljCyqq0gqk0Mc=
github.com/aws/aws-sdk-go-v2 v1.48.0/go.mod h1:oeKU5p+TQBnpeLmU42WZLWvzv+ORLn+74n4QVLnm/Z0=
github.com/aws/aws-sdk-go-v2/config v1.33.19 h1:M0aPu5EdAMR6lGYN2GvM76WY0yqTyDi+gxV7s7Cdnr8=
github.com/aws/aws-sdk-go-v2/config v1.33.19/go.mod h1:FSZkRx5fOaq82bZkGZrK+dXFgWvI4iIE0Y6ADmEKDag=
github.com/aws/aws-sdk-go-v2/credentials v1.21.1 h1:4iuMmYSa42WMeClXjzE+HoZIeBud6o/bd00BN6WM9H4=
github.com/aws/aws-sdk-go-v2/credentials v1.21.1/go.mod h1:OffaVQod3fyZzhZJyPtw11WpHPF1uIHADc+/MYlXmbA=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.9 h1:oH1E0tjN0g+ZRusbX1dsuat4FJ0z05l8hWaycSbDuNc=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.9/go.mod h1:3ZaQGigIoaS45x9HgnLZ4sP475Fs7QoxU3IsZX/J3h4=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.12 h1:W0yUUzKBA8SofGQ0yZafEUuZq9UxfvijRgdc6vcRQPA=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.12/go.mod h1:xwKv6vwBNdpPFOxEm/ld4IODZK2s/1ksHvdBqLfH6Gs=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.12 h1:lqSi8qvAnKC1vfosImdmOsuiJLr/jdybZX5299beB6Q=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.12/go.mod h1:rqPQE2vqkW9YHx5+Gp55sx1wsTM4xtGQLl+TKMjxxrs=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.12 h1:uvX2yaL+aMjkmnrLPPlR/zwjoKFRMoNhhJfeGjfk6qc=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.12/go.mod h1:dicT+2eNhHHqGkxHO7LoXw3QghIUGuzgdmFqaSTopgI=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.25 h1:rKwkY3Z+fvt2W0msjdNs6uNewPmXzPayQ961oyuWhes=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.25/go.mod h1:dBuAadQKd1F4Q4ZOg8gnFmuSHMRE40Pe3IfFwbtC4lE=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.12 h1:wzAqMZ2O4+ij9HQqIrk+P9PBGacE0wOTuWB7Mo97//I=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.12/go.mod h1:/0TfyruByHsOlphzP26xDTRe2DKD0J1I1Uyy2TyP8EI=
github.com/aws/aws-sdk-go-v2/service/kms v1.63.0 h1:Ovjb0ZSWfm+hPYFgU9BX0Oy7C/ob3EXbCrUUHo9Is98=
github.com/aws/aws-sdk-go-v2/service/kms v1.63.0/go.mod h1:VMuZyNobzmV/0SRCH5p+0dziD4U5AktOi3TzFMXq1rg=
github.com/aws/aws-sdk-go-v2/service/signin v1.13.1 h1:ssrDA6AKfXEc3h0Pf4EI4vQ8sN5Zd136SrU9Tjr0XPc=
github.com/aws/aws-sdk-go-v2/service/signin v1.13.1/go.mod h1:LdwHjKqPE9VCC7sTlWcOAizZx3ZJYjmfwC/EW2tIIAE=
github.com/aws/aws-sdk-go-v2/service/sso v1.39.5 h1:ANAoa+qt3/9kXnvQVgUyxcTzhzZf4DqaBS+M2DQrELU=
github.com/aws/aws-sdk-go-v2/service/sso v1.39.5/go.mod h1:vsMi+SSR7xGQXtV430LVi49qbv/R+U+upHHSp1W/+QI=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.5 h1:uIOBLMhef4N16WguadoHKuBy9etdz+RKTeqzchXErAI=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.5/go.mod h1:NwWmkFLtsTVyXQpbzPOlYLwEoANkdCFjJRtrNcbg/vY=
github.com/aws/aws-sdk-go-v2/service/sts v1.52.0 h1:IaBLmJWiIEtKkjXMgMbYGZk7WKXbYA8148wCMCXq9hU=
github.com/aws/aws-sdk-go-v2/service/sts v1.52.0/go.mod h1:MG6N8U9+lY4LqchrlHPqCOSr7hyxw70XU0Et2YR3eNA=
github.com/aws/smithy-go v1.28.9 h1:zkZ058OpNX0iGTK24b+ZUEQSMc11CBA6oO7xtKZfzkY=
github.com/aws/smithy-go v1.28.9/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
github.com/bufbuild/protocompile v0.14.1 h1:iA73zAf/fyljNjQKwYzUHD6AD4R8KMasmwa/FBatYVw=
github.com/bufbuild/protocompile v0.14.1/go.mod h1:ppVdAIhbr2H8asPk6k4pY7t9zB1OU5DoEw9xY/FUi1c=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
Expand Down
94 changes: 94 additions & 0 deletions internal/cmd/kmspgp/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Command kmspgp signs a release's checksums with the provider's KMS key, as OpenPGP, and prints
// that key's public half for the registries:
//
// kmspgp -key alias/terraform-provider-spin-signing public # the armoured key, once per registry
// kmspgp -key alias/terraform-provider-spin-signing sign -out SHA256SUMS.sig SHA256SUMS
//
// AWS credentials are the SDK's own: the release workflow's role (spin-stack/ci's signing.tf).
package main

import (
"context"
"errors"
"flag"
"fmt"
"io"
"os"
"time"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/kms"

"github.com/spin-stack/terraform-provider-spin/internal/kmspgp"
)

func main() {
if err := run(context.Background(), os.Args[1:], os.Stdout, newKMS); err != nil { // mutate-exempt: the process's own arguments; run is what the tests drive
fmt.Fprintln(os.Stderr, "kmspgp:", err) //nolint:errcheck // the exit status says it
os.Exit(1) // mutate-exempt: any status but 0 fails the release; run is what the tests drive
}
}

func newKMS(ctx context.Context) (kmspgp.KMS, error) {
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil { // mutate-exempt: reached only with AWS's own configuration, which the release's role is
return nil, fmt.Errorf("AWS credentials: %w", err)
}
return kmspgp.AWS{Client: kms.NewFromConfig(cfg)}, nil
}

func run(ctx context.Context, args []string, stdout io.Writer, connect func(context.Context) (kmspgp.KMS, error)) error {
fs := flag.NewFlagSet("kmspgp", flag.ContinueOnError)
keyID := fs.String("key", "", "the KMS key: its id, ARN or alias")
if err := fs.Parse(args); err != nil {
return err
}
if *keyID == "" || fs.NArg() == 0 {
return errors.New("usage: kmspgp -key <kms key> public | sign -out <signature> <file>")
}
k, err := connect(ctx)
if err != nil {
return err
}
signer, err := kmspgp.NewKMSSigner(ctx, k, *keyID)
if err != nil {
return err
}
key, err := kmspgp.New(signer, kmspgp.Created)
if err != nil {
return err
}
switch fs.Arg(0) {
case "public":
if _, err := fmt.Fprintf(stdout, "# %s, fingerprint %s\n", kmspgp.UserID, key.Fingerprint()); err != nil {
return err
}
return key.WritePublic(stdout)
case "sign":
return sign(key, fs.Args()[1:])
default:
return fmt.Errorf("%q is neither public nor sign", fs.Arg(0))
}
}

func sign(key kmspgp.Key, args []string) (err error) {
fs := flag.NewFlagSet("sign", flag.ContinueOnError)
out := fs.String("out", "", "where the detached signature is written")
if err := fs.Parse(args); err != nil {
return err
}
if *out == "" || fs.NArg() != 1 {
return errors.New("usage: kmspgp -key <kms key> sign -out <signature> <file>")
}
in, err := os.Open(fs.Arg(0))
if err != nil {
return err
}
defer func() { _ = in.Close() }() // read only
w, err := os.Create(*out)
if err != nil {
return err
}
defer func() { err = errors.Join(err, w.Close()) }()
return key.Sign(w, in, time.Now())
}
106 changes: 106 additions & 0 deletions internal/cmd/kmspgp/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package main

import (
"bytes"
"context"
"crypto"
"crypto/rand"
"crypto/rsa"
"errors"
"os"
"path/filepath"
"strings"
"testing"

"github.com/ProtonMail/go-crypto/openpgp"

"github.com/spin-stack/terraform-provider-spin/internal/kmspgp"
)

type localKMS struct{ key *rsa.PrivateKey }

func (l localKMS) Sign(_ context.Context, _ string, digest []byte) ([]byte, error) {
return rsa.SignPKCS1v15(nil, l.key, crypto.SHA256, digest)
}

func (l localKMS) PublicKey(context.Context, string) (*rsa.PublicKey, error) {
return &l.key.PublicKey, nil
}

// The public key is printed for the registries, and a file signed with it is checked by it.
func TestThePublicKeySignsAndChecks(t *testing.T) {
k, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
connect := func(context.Context) (kmspgp.KMS, error) { return localKMS{k}, nil }
var pub bytes.Buffer
if err := run(t.Context(), []string{"-key", "alias/k", "public"}, &pub, connect); err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(pub.String(), "# "+kmspgp.UserID+", fingerprint ") || !strings.Contains(pub.String(), "BEGIN PGP PUBLIC KEY BLOCK") {
t.Fatalf("printed %s", pub.String())
}
dir := t.TempDir()
sums := filepath.Join(dir, "SHA256SUMS")
if err := os.WriteFile(sums, []byte("sums\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := run(t.Context(), []string{"-key", "alias/k", "sign", "-out", sums + ".sig", sums}, &bytes.Buffer{}, connect); err != nil {
t.Fatal(err)
}
ring, err := openpgp.ReadArmoredKeyRing(strings.NewReader(pub.String()[strings.Index(pub.String(), "-----BEGIN"):]))
if err != nil {
t.Fatal(err)
}
sig, err := os.ReadFile(sums + ".sig")
if err != nil {
t.Fatal(err)
}
if _, err := openpgp.CheckDetachedSignature(ring, strings.NewReader("sums\n"), bytes.NewReader(sig), nil); err != nil {
t.Fatal(err)
}
}

// What is not a whole command signs nothing and says so.
func TestAnIncompleteCommandIsRefused(t *testing.T) {
k, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
connect := func(context.Context) (kmspgp.KMS, error) { return localKMS{k}, nil }
file := filepath.Join(t.TempDir(), "f")
if err := os.WriteFile(file, []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
for name, args := range map[string][]string{
"no out, said": {"-key", "alias/k", "sign", file},
"two files": {"-key", "alias/k", "sign", "-out", file + ".sig", file, file},
"no key": {"public"},
"no verb": {"-key", "alias/k"},
"another verb": {"-key", "alias/k", "verify"},
"no out": {"-key", "alias/k", "sign", file},
"no file": {"-key", "alias/k", "sign", "-out", file + ".sig"},
"a missing file": {"-key", "alias/k", "sign", "-out", file + ".sig", file + ".none"},
"no such flag": {"-nope"},
"a bad sign flag": {"-key", "alias/k", "sign", "-nope"},
"an unwritable signature": {"-key", "alias/k", "sign", "-out", filepath.Join(file, "x.sig"), file},
} {
t.Run(name, func(t *testing.T) {
err := run(t.Context(), args, &bytes.Buffer{}, connect)
if err == nil {
t.Fatal("ran")
}
if strings.HasSuffix(name, ", said") || name == "two files" {
if !strings.Contains(err.Error(), "usage: kmspgp -key <kms key> sign") {
t.Fatalf("err = %v, want the sign usage", err)
}
}
})
}
if err := run(t.Context(), []string{"-key", "alias/k", "public"}, &bytes.Buffer{}, func(context.Context) (kmspgp.KMS, error) {
return nil, errors.New("no credentials")
}); err == nil || !strings.Contains(err.Error(), "no credentials") {
t.Fatalf("err = %v", err)
}
}
Loading
Loading