|
| 1 | +/* |
| 2 | +Copyright 2024 The Flux 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 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/fluxcd/flux2/v2/internal/utils" |
| 25 | + "github.com/fluxcd/flux2/v2/pkg/manifestgen/sourcesecret" |
| 26 | + "github.com/spf13/cobra" |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + "sigs.k8s.io/yaml" |
| 29 | +) |
| 30 | + |
| 31 | +var createSecretGitHubAppCmd = &cobra.Command{ |
| 32 | + Use: "githubapp [name]", |
| 33 | + Short: "Create or update a github app secret", |
| 34 | + Long: withPreviewNote(`The create secret githubapp command generates a Kubernetes secret that can be used for GitRepository authentication with github app`), |
| 35 | + Example: ` # Create a githubapp authentication secret on disk and encrypt it with Mozilla SOPS |
| 36 | + flux create secret githubapp podinfo-auth \ |
| 37 | + --app-id="1" \ |
| 38 | + --app-installation-id="2" \ |
| 39 | + --app-private-key=./private-key-file.pem \ |
| 40 | + --export > githubapp-auth.yaml |
| 41 | +
|
| 42 | + sops --encrypt --encrypted-regex '^(data|stringData)$' \ |
| 43 | + --in-place githubapp-auth.yaml |
| 44 | + `, |
| 45 | + RunE: createSecretGitHubAppCmdRun, |
| 46 | +} |
| 47 | + |
| 48 | +type secretGitHubAppFlags struct { |
| 49 | + appID string |
| 50 | + appInstallationID string |
| 51 | + privateKeyFile string |
| 52 | + baseURL string |
| 53 | +} |
| 54 | + |
| 55 | +var secretGitHubAppArgs = secretGitHubAppFlags{} |
| 56 | + |
| 57 | +func init() { |
| 58 | + createSecretGitHubAppCmd.Flags().StringVar(&secretGitHubAppArgs.appID, "app-id", "", "github app ID") |
| 59 | + createSecretGitHubAppCmd.Flags().StringVar(&secretGitHubAppArgs.appInstallationID, "app-installation-id", "", "github app installation ID") |
| 60 | + createSecretGitHubAppCmd.Flags().StringVar(&secretGitHubAppArgs.privateKeyFile, "app-private-key", "", "github app private key file path") |
| 61 | + createSecretGitHubAppCmd.Flags().StringVar(&secretGitHubAppArgs.baseURL, "app-base-url", "", "github app base URL") |
| 62 | + |
| 63 | + createSecretCmd.AddCommand(createSecretGitHubAppCmd) |
| 64 | +} |
| 65 | + |
| 66 | +func createSecretGitHubAppCmdRun(cmd *cobra.Command, args []string) error { |
| 67 | + if len(args) < 1 { |
| 68 | + return fmt.Errorf("name is required") |
| 69 | + } |
| 70 | + |
| 71 | + secretName := args[0] |
| 72 | + |
| 73 | + if secretGitHubAppArgs.appID == "" { |
| 74 | + return fmt.Errorf("--app-id is required") |
| 75 | + } |
| 76 | + |
| 77 | + if secretGitHubAppArgs.appInstallationID == "" { |
| 78 | + return fmt.Errorf("--app-installation-id is required") |
| 79 | + } |
| 80 | + |
| 81 | + if secretGitHubAppArgs.privateKeyFile == "" { |
| 82 | + return fmt.Errorf("--app-private-key is required") |
| 83 | + } |
| 84 | + |
| 85 | + privateKey, err := os.ReadFile(secretGitHubAppArgs.privateKeyFile) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("unable to read private key file: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + opts := sourcesecret.Options{ |
| 91 | + Name: secretName, |
| 92 | + Namespace: *kubeconfigArgs.Namespace, |
| 93 | + GitHubAppID: secretGitHubAppArgs.appID, |
| 94 | + GitHubAppInstallationID: secretGitHubAppArgs.appInstallationID, |
| 95 | + GitHubAppPrivateKey: string(privateKey), |
| 96 | + } |
| 97 | + |
| 98 | + if secretGitHubAppArgs.baseURL != "" { |
| 99 | + opts.GitHubAppBaseURL = secretGitHubAppArgs.baseURL |
| 100 | + } |
| 101 | + |
| 102 | + secret, err := sourcesecret.Generate(opts) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + if createArgs.export { |
| 108 | + rootCmd.Println(secret.Content) |
| 109 | + return nil |
| 110 | + } |
| 111 | + |
| 112 | + ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) |
| 113 | + defer cancel() |
| 114 | + kubeClient, err := utils.KubeClient(kubeconfigArgs, kubeclientOptions) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + var s corev1.Secret |
| 119 | + if err := yaml.Unmarshal([]byte(secret.Content), &s); err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + if err := upsertSecret(ctx, kubeClient, s); err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + logger.Actionf("githubapp secret '%s' created in '%s' namespace", secretName, *kubeconfigArgs.Namespace) |
| 127 | + return nil |
| 128 | +} |
0 commit comments