|
| 1 | +// Copyright Cozystack 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 commands |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/cozystack/talm/pkg/age" |
| 24 | + clientconfig "github.com/siderolabs/talos/pkg/machinery/client/config" |
| 25 | + "github.com/siderolabs/talos/pkg/machinery/config/generate/secrets" |
| 26 | + "gopkg.in/yaml.v3" |
| 27 | +) |
| 28 | + |
| 29 | +// SaveTalosconfigWithEncryption saves talosconfig and updates talosconfig.encrypted if it exists. |
| 30 | +func SaveTalosconfigWithEncryption(config *clientconfig.Config, talosconfigPath string) error { |
| 31 | + // Save the talosconfig |
| 32 | + if err := config.Save(talosconfigPath); err != nil { |
| 33 | + return fmt.Errorf("failed to save talosconfig: %w", err) |
| 34 | + } |
| 35 | + |
| 36 | + // Update talosconfig.encrypted if it exists |
| 37 | + encryptedPath := filepath.Join(Config.RootDir, "talosconfig.encrypted") |
| 38 | + if fileExists(encryptedPath) { |
| 39 | + fmt.Fprintf(os.Stderr, "Updating talosconfig.encrypted...\n") |
| 40 | + if err := age.EncryptYAMLFile(Config.RootDir, "talosconfig", "talosconfig.encrypted"); err != nil { |
| 41 | + return fmt.Errorf("failed to encrypt talosconfig: %w", err) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return nil |
| 46 | +} |
| 47 | + |
| 48 | +// UpdateKubeconfigEncryption updates kubeconfig.encrypted if it exists. |
| 49 | +// kubeconfigPath should be an absolute path to the kubeconfig file. |
| 50 | +// Returns nil if encrypted file doesn't exist or key is missing. |
| 51 | +func UpdateKubeconfigEncryption(kubeconfigPath string) error { |
| 52 | + // Get relative path from project root |
| 53 | + rootAbs, err := filepath.Abs(Config.RootDir) |
| 54 | + if err != nil { |
| 55 | + return nil // Skip encryption if we can't get absolute path |
| 56 | + } |
| 57 | + |
| 58 | + relKubeconfigPath, err := filepath.Rel(rootAbs, kubeconfigPath) |
| 59 | + if err != nil || strings.HasPrefix(relKubeconfigPath, "..") { |
| 60 | + return nil // Skip encryption if path is outside project root |
| 61 | + } |
| 62 | + |
| 63 | + encryptedKubeconfigPath := relKubeconfigPath + ".encrypted" |
| 64 | + encryptedKubeconfigFile := filepath.Join(Config.RootDir, encryptedKubeconfigPath) |
| 65 | + keyFile := filepath.Join(Config.RootDir, "talm.key") |
| 66 | + |
| 67 | + if !fileExists(encryptedKubeconfigFile) || !fileExists(keyFile) { |
| 68 | + return nil // Skip if encrypted file or key doesn't exist |
| 69 | + } |
| 70 | + |
| 71 | + fmt.Fprintf(os.Stderr, "Updating %s...\n", encryptedKubeconfigPath) |
| 72 | + if err := age.EncryptYAMLFile(Config.RootDir, relKubeconfigPath, encryptedKubeconfigPath); err != nil { |
| 73 | + return fmt.Errorf("failed to encrypt kubeconfig: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// UpdateTalosconfigEncryption updates talosconfig.encrypted if it exists. |
| 80 | +// Returns nil if encrypted file doesn't exist or key is missing. |
| 81 | +func UpdateTalosconfigEncryption() error { |
| 82 | + encryptedPath := filepath.Join(Config.RootDir, "talosconfig.encrypted") |
| 83 | + keyFile := filepath.Join(Config.RootDir, "talm.key") |
| 84 | + |
| 85 | + if !fileExists(encryptedPath) || !fileExists(keyFile) { |
| 86 | + return nil // Skip if encrypted file or key doesn't exist |
| 87 | + } |
| 88 | + |
| 89 | + fmt.Fprintf(os.Stderr, "Updating talosconfig.encrypted...\n") |
| 90 | + if err := age.EncryptYAMLFile(Config.RootDir, "talosconfig", "talosconfig.encrypted"); err != nil { |
| 91 | + return fmt.Errorf("failed to encrypt talosconfig: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +// SaveSecretsBundleWithEncryption saves secrets.yaml and updates secrets.encrypted.yaml if it exists. |
| 98 | +func SaveSecretsBundleWithEncryption(bundle *secrets.Bundle) error { |
| 99 | + secretsPath := ResolveSecretsPath(Config.TemplateOptions.WithSecrets) |
| 100 | + |
| 101 | + // Marshal the bundle |
| 102 | + data, err := yaml.Marshal(bundle) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("failed to marshal secrets: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + // Save secrets.yaml |
| 108 | + if err := os.WriteFile(secretsPath, data, 0o600); err != nil { |
| 109 | + return fmt.Errorf("failed to write secrets.yaml: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + // Update secrets.encrypted.yaml if it exists |
| 113 | + encryptedPath := filepath.Join(Config.RootDir, "secrets.encrypted.yaml") |
| 114 | + if fileExists(encryptedPath) { |
| 115 | + fmt.Fprintf(os.Stderr, "Updating secrets.encrypted.yaml...\n") |
| 116 | + if err := age.EncryptSecretsFile(Config.RootDir); err != nil { |
| 117 | + return fmt.Errorf("failed to encrypt secrets.yaml: %w", err) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
0 commit comments