|
7 | 7 |
|
8 | 8 | "github.com/spf13/cobra" |
9 | 9 |
|
10 | | - "github.com/stacklok/toolhive/pkg/certs" |
11 | 10 | "github.com/stacklok/toolhive/pkg/config" |
12 | 11 | ) |
13 | 12 |
|
@@ -99,73 +98,51 @@ func init() { |
99 | 98 | } |
100 | 99 |
|
101 | 100 | func setCACertCmdFunc(_ *cobra.Command, args []string) error { |
102 | | - certPath := filepath.Clean(args[0]) |
| 101 | + certPath := args[0] |
103 | 102 |
|
104 | | - // Validate that the file exists and is readable |
105 | | - if _, err := os.Stat(certPath); err != nil { |
106 | | - return fmt.Errorf("CA certificate file not found or not accessible: %w", err) |
107 | | - } |
108 | | - |
109 | | - // Read and validate the certificate |
110 | | - certContent, err := os.ReadFile(certPath) |
111 | | - if err != nil { |
112 | | - return fmt.Errorf("failed to read CA certificate file: %w", err) |
113 | | - } |
114 | | - |
115 | | - // Validate the certificate format |
116 | | - if err := certs.ValidateCACertificate(certContent); err != nil { |
117 | | - return fmt.Errorf("invalid CA certificate: %w", err) |
118 | | - } |
119 | | - |
120 | | - // Update the configuration |
121 | | - err = config.UpdateConfig(func(c *config.Config) { |
122 | | - c.CACertificatePath = certPath |
123 | | - }) |
| 103 | + provider := config.NewDefaultProvider() |
| 104 | + err := provider.SetCACert(certPath) |
124 | 105 | if err != nil { |
125 | | - return fmt.Errorf("failed to update configuration: %w", err) |
| 106 | + return err |
126 | 107 | } |
127 | 108 |
|
128 | | - fmt.Printf("Successfully set CA certificate path: %s\n", certPath) |
| 109 | + fmt.Printf("Successfully set CA certificate path: %s\n", filepath.Clean(certPath)) |
129 | 110 | return nil |
130 | 111 | } |
131 | 112 |
|
132 | 113 | func getCACertCmdFunc(_ *cobra.Command, _ []string) error { |
133 | | - configProvider := config.NewDefaultProvider() |
134 | | - cfg := configProvider.GetConfig() |
| 114 | + provider := config.NewDefaultProvider() |
| 115 | + certPath, exists, accessible := provider.GetCACert() |
135 | 116 |
|
136 | | - if cfg.CACertificatePath == "" { |
| 117 | + if !exists { |
137 | 118 | fmt.Println("No CA certificate is currently configured.") |
138 | 119 | return nil |
139 | 120 | } |
140 | 121 |
|
141 | | - fmt.Printf("Current CA certificate path: %s\n", cfg.CACertificatePath) |
| 122 | + fmt.Printf("Current CA certificate path: %s\n", certPath) |
142 | 123 |
|
143 | | - // Check if the file still exists |
144 | | - if _, err := os.Stat(cfg.CACertificatePath); err != nil { |
145 | | - fmt.Printf("Warning: The configured CA certificate file is not accessible: %v\n", err) |
| 124 | + if !accessible { |
| 125 | + fmt.Printf("Warning: The configured CA certificate file is not accessible\n") |
146 | 126 | } |
147 | 127 |
|
148 | 128 | return nil |
149 | 129 | } |
150 | 130 |
|
151 | 131 | func unsetCACertCmdFunc(_ *cobra.Command, _ []string) error { |
152 | | - configProvider := config.NewDefaultProvider() |
153 | | - cfg := configProvider.GetConfig() |
| 132 | + provider := config.NewDefaultProvider() |
| 133 | + certPath, exists, _ := provider.GetCACert() |
154 | 134 |
|
155 | | - if cfg.CACertificatePath == "" { |
| 135 | + if !exists { |
156 | 136 | fmt.Println("No CA certificate is currently configured.") |
157 | 137 | return nil |
158 | 138 | } |
159 | 139 |
|
160 | | - // Update the configuration |
161 | | - err := config.UpdateConfig(func(c *config.Config) { |
162 | | - c.CACertificatePath = "" |
163 | | - }) |
| 140 | + err := provider.UnsetCACert() |
164 | 141 | if err != nil { |
165 | | - return fmt.Errorf("failed to update configuration: %w", err) |
| 142 | + return err |
166 | 143 | } |
167 | 144 |
|
168 | | - fmt.Println("Successfully removed CA certificate configuration.") |
| 145 | + fmt.Printf("Successfully removed CA certificate configuration: %s\n", certPath) |
169 | 146 | return nil |
170 | 147 | } |
171 | 148 |
|
|
0 commit comments