|
| 1 | +// |
| 2 | +// Copyright 2025 The Sigstore Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package app |
| 17 | + |
| 18 | +import ( |
| 19 | + "crypto" |
| 20 | + "fmt" |
| 21 | + "log/slog" |
| 22 | + "time" |
| 23 | + |
| 24 | + clog "github.com/chainguard-dev/clog/gcp" |
| 25 | + grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry" |
| 26 | + "github.com/spf13/cobra" |
| 27 | + "github.com/spf13/viper" |
| 28 | + "google.golang.org/api/option" |
| 29 | + "google.golang.org/grpc" |
| 30 | + |
| 31 | + "github.com/sigstore/rekor-tiles/v2/internal/signerverifier" |
| 32 | + "github.com/sigstore/rekor-tiles/v2/internal/tessera" |
| 33 | + "github.com/sigstore/sigstore/pkg/signature" |
| 34 | + "github.com/sigstore/sigstore/pkg/signature/kms/gcp" |
| 35 | +) |
| 36 | + |
| 37 | +// GCPBackend implements the BackendConfig interface for GCP |
| 38 | +type GCPBackend struct{} |
| 39 | + |
| 40 | +func (g *GCPBackend) Name() string { |
| 41 | + return "gcp" |
| 42 | +} |
| 43 | + |
| 44 | +func (g *GCPBackend) Description() string { |
| 45 | + return "Google Cloud Platform" |
| 46 | +} |
| 47 | + |
| 48 | +func (g *GCPBackend) SetupLogger(logLevel slog.Level) *slog.Logger { |
| 49 | + return slog.New(clog.NewHandler(logLevel)) |
| 50 | +} |
| 51 | + |
| 52 | +func (g *GCPBackend) RegisterFlags(cmd *cobra.Command) { |
| 53 | + // GCP-specific storage configs |
| 54 | + cmd.Flags().String("gcp-bucket", "", "GCS bucket for tile and checkpoint storage") |
| 55 | + cmd.Flags().String("gcp-spanner", "", "Spanner database URI") |
| 56 | + |
| 57 | + // GCP KMS configs |
| 58 | + cmd.Flags().String("signer-kmskey", "", "URI of the KMS key, in the form of gcpkms://keyname") |
| 59 | + cmd.Flags().String("signer-tink-kek-uri", "", "encryption key for decrypting Tink keyset. Valid options are [gcp-kms://keyname]") |
| 60 | + cmd.Flags().Uint("gcp-kms-retries", 0, "number of retries for GCP KMS requests") |
| 61 | + cmd.Flags().Uint32("gcp-kms-timeout", 0, "sets the RPC timeout per call for GCP KMS requests in seconds, defaults to 0 (no timeout)") |
| 62 | +} |
| 63 | + |
| 64 | +func (g *GCPBackend) GetKMSSignerOptions() ([]signerverifier.Option, error) { |
| 65 | + kmshash := viper.GetString("signer-kmshash") |
| 66 | + hashAlg, ok := hashAlgMap[kmshash] |
| 67 | + if !ok { |
| 68 | + return nil, fmt.Errorf("invalid hash algorithm for --signer-kmshash: %s", kmshash) |
| 69 | + } |
| 70 | + // initialize optional RPC options for GCP KMS |
| 71 | + rpcOpts := make([]signature.RPCOption, 0) |
| 72 | + callOpts := []grpc_retry.CallOption{grpc_retry.WithMax(viper.GetUint("gcp-kms-retries")), grpc_retry.WithPerRetryTimeout(time.Duration(viper.GetUint32("gcp-kms-timeout")) * time.Second)} |
| 73 | + rpcOpts = append(rpcOpts, gcp.WithGoogleAPIClientOption(option.WithGRPCDialOption(grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor(callOpts...))))) |
| 74 | + |
| 75 | + return []signerverifier.Option{signerverifier.WithKMS(viper.GetString("signer-kmskey"), hashAlg, rpcOpts)}, nil |
| 76 | +} |
| 77 | + |
| 78 | +func (g *GCPBackend) GetDriverConfig() (tessera.DriverConfiguration, error) { |
| 79 | + return tessera.DriverConfiguration{ |
| 80 | + Hostname: viper.GetString("hostname"), |
| 81 | + GCPBucket: viper.GetString("gcp-bucket"), |
| 82 | + GCPSpannerDB: viper.GetString("gcp-spanner"), |
| 83 | + PersistentAntispam: viper.GetBool("persistent-antispam"), |
| 84 | + ASMaxBatchSize: viper.GetUint("antispam-max-batch-size"), |
| 85 | + ASPushbackThreshold: viper.GetUint("antispam-pushback-threshold"), |
| 86 | + }, nil |
| 87 | +} |
| 88 | + |
| 89 | +var hashAlgMap = map[string]crypto.Hash{ |
| 90 | + "sha256": crypto.SHA256, |
| 91 | + "sha384": crypto.SHA384, |
| 92 | + "sha512": crypto.SHA512, |
| 93 | +} |
0 commit comments