go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/client/cmd/cloudkms/download.go (about) 1 // Copyright 2020 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "context" 19 20 "github.com/maruel/subcommands" 21 22 cloudkms "cloud.google.com/go/kms/apiv1" 23 "cloud.google.com/go/kms/apiv1/kmspb" 24 25 "go.chromium.org/luci/auth" 26 "go.chromium.org/luci/common/logging" 27 ) 28 29 func doDownload(ctx context.Context, client *cloudkms.KeyManagementClient, keyPath string) ([]byte, error) { 30 resp, err := client.GetPublicKey(ctx, &kmspb.GetPublicKeyRequest{Name: keyPath}) 31 if err != nil { 32 logging.Errorf(ctx, "Error while making request") 33 return nil, err 34 } 35 36 return []byte(resp.Pem), nil 37 } 38 39 func cmdDownload(authOpts auth.Options) *subcommands.Command { 40 return &subcommands.Command{ 41 UsageLine: "download <options> <path>", 42 ShortDesc: "downloads public keys from Cloud KMS", 43 LongDesc: `Downloads a public key from Cloud KMS. 44 45 <path> refers to the path to the crypto key. e.g. 46 47 projects/<project>/locations/<location>/keyRings/<keyRing>/cryptoKeys/<cryptoKey>/cryptoKeyVersions/<version> 48 49 -output will be the public key`, 50 CommandRun: func() subcommands.CommandRun { 51 c := downloadRun{doDownload: doDownload} 52 c.Init(authOpts) 53 return &c 54 }, 55 } 56 }