Skip to content

Commit f4f77a3

Browse files
authored
feat: use host ca bundle for PrivateCACert template function (#2208)
* feat: use host ca bundle for PrivateCACert template function * f * f * f * f * f * f * f * f * f * f * f
1 parent e4f517e commit f4f77a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1185
-475
lines changed

.github/workflows/ci.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,6 @@ jobs:
781781
is-lxd: true
782782
- test: TestProxiedCustomCIDR
783783
is-lxd: true
784-
- test: TestInstallWithPrivateCAs
785-
is-lxd: true
786784
- test: TestInstallWithMITMProxy
787785
is-lxd: true
788786
steps:

.github/workflows/release-prod.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,6 @@ jobs:
582582
is-lxd: true
583583
- test: TestProxiedCustomCIDR
584584
is-lxd: true
585-
- test: TestInstallWithPrivateCAs
586-
is-lxd: true
587585
- test: TestInstallWithMITMProxy
588586
is-lxd: true
589587
steps:

api/api_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/replicatedhq/embedded-cluster/api/types"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestAPI_jsonError(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
apiErr *types.APIError
17+
wantCode int
18+
wantJSON map[string]any
19+
}{
20+
{
21+
name: "simple error",
22+
apiErr: &types.APIError{
23+
StatusCode: http.StatusInternalServerError,
24+
Message: "invalid request",
25+
},
26+
wantCode: http.StatusInternalServerError,
27+
wantJSON: map[string]any{
28+
"status_code": float64(http.StatusInternalServerError),
29+
"message": "invalid request",
30+
},
31+
},
32+
{
33+
name: "field error",
34+
apiErr: &types.APIError{
35+
StatusCode: http.StatusBadRequest,
36+
Message: "validation error",
37+
Field: "username",
38+
},
39+
wantCode: http.StatusBadRequest,
40+
wantJSON: map[string]any{
41+
"status_code": float64(http.StatusBadRequest),
42+
"message": "validation error",
43+
"field": "username",
44+
},
45+
},
46+
{
47+
name: "error with nested errors",
48+
apiErr: &types.APIError{
49+
StatusCode: http.StatusBadRequest,
50+
Message: "multiple validation errors",
51+
Errors: []*types.APIError{
52+
{
53+
Message: "field1 is required",
54+
Field: "field1",
55+
},
56+
{
57+
Message: "field2 must be a number",
58+
Field: "field2",
59+
},
60+
},
61+
},
62+
wantCode: http.StatusBadRequest,
63+
wantJSON: map[string]any{
64+
"status_code": float64(http.StatusBadRequest),
65+
"message": "multiple validation errors",
66+
"errors": []any{
67+
map[string]any{
68+
"message": "field1 is required",
69+
"field": "field1",
70+
},
71+
map[string]any{
72+
"message": "field2 must be a number",
73+
"field": "field2",
74+
},
75+
},
76+
},
77+
},
78+
}
79+
80+
for _, tt := range tests {
81+
t.Run(tt.name, func(t *testing.T) {
82+
// Create a mock HTTP response recorder
83+
rec := httptest.NewRecorder()
84+
85+
// Call the JSON method
86+
api := &API{
87+
logger: NewDiscardLogger(),
88+
}
89+
api.jsonError(rec, httptest.NewRequest("GET", "/api/test", nil), tt.apiErr)
90+
91+
// Check status code
92+
assert.Equal(t, tt.wantCode, rec.Code, "Status code should match")
93+
94+
// Check content type header
95+
contentType := rec.Header().Get("Content-Type")
96+
assert.Equal(t, "application/json", contentType, "Content-Type header should be application/json")
97+
98+
// Parse and check the JSON response
99+
var gotJSON map[string]any
100+
err := json.Unmarshal(rec.Body.Bytes(), &gotJSON)
101+
assert.NoError(t, err, "Should be able to parse the JSON response")
102+
assert.Equal(t, tt.wantJSON, gotJSON, "JSON response should match expected structure")
103+
})
104+
}
105+
}

cmd/installer/cli/enable_ha.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,5 @@ func runEnableHA(ctx context.Context) error {
9191
loading := spinner.Start()
9292
defer loading.Close()
9393

94-
return addons.EnableHA(ctx, kcli, kclient, hcli, in.Spec.Network.ServiceCIDR, in.Spec, loading)
94+
return addons.EnableHA(ctx, logrus.Debugf, kcli, kclient, hcli, in.Spec.Network.ServiceCIDR, in.Spec, loading)
9595
}

cmd/installer/cli/install.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ type InstallCmdFlags struct {
7676
localArtifactMirrorPort int
7777
assumeYes bool
7878
overrides string
79-
privateCAs []string
8079
skipHostPreflights bool
8180
ignoreHostPreflights bool
8281
configValues string
@@ -185,7 +184,13 @@ func addInstallFlags(cmd *cobra.Command, flags *InstallCmdFlags) error {
185184
return err
186185
}
187186

188-
cmd.Flags().StringSliceVar(&flags.privateCAs, "private-ca", []string{}, "Path to a trusted private CA certificate file")
187+
cmd.Flags().StringSlice("private-ca", []string{}, "Path to a trusted private CA certificate file")
188+
if err := cmd.Flags().MarkHidden("private-ca"); err != nil {
189+
return err
190+
}
191+
if err := cmd.Flags().MarkDeprecated("private-ca", "This flag is no longer used and will be removed in a future version. The CA bundle will be automatically detected from the host."); err != nil {
192+
return err
193+
}
189194

190195
if err := addProxyFlags(cmd); err != nil {
191196
return err
@@ -602,13 +607,12 @@ func runInstall(ctx context.Context, name string, flags InstallCmdFlags, metrics
602607
defer hcli.Close()
603608

604609
logrus.Debugf("installing addons")
605-
if err := addons.Install(ctx, hcli, addons.InstallOptions{
610+
if err := addons.Install(ctx, logrus.Debugf, hcli, addons.InstallOptions{
606611
AdminConsolePwd: flags.adminConsolePassword,
607612
License: flags.license,
608613
IsAirgap: flags.airgapBundle != "",
609614
Proxy: flags.proxy,
610615
HostCABundlePath: runtimeconfig.HostCABundlePath(),
611-
PrivateCAs: flags.privateCAs,
612616
TLSCertBytes: flags.tlsCertBytes,
613617
TLSKeyBytes: flags.tlsKeyBytes,
614618
Hostname: flags.hostname,

cmd/installer/cli/install_runpreflights.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ func runInstallPreflights(ctx context.Context, flags InstallCmdFlags, metricsRep
9999
ServiceCIDR: flags.cidrCfg.ServiceCIDR,
100100
GlobalCIDR: flags.cidrCfg.GlobalCIDR,
101101
NodeIP: nodeIP,
102-
PrivateCAs: flags.privateCAs,
103102
IsAirgap: flags.isAirgap,
104103
SkipHostPreflights: flags.skipHostPreflights,
105104
IgnoreHostPreflights: flags.ignoreHostPreflights,

cmd/installer/cli/join.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ func maybeEnableHA(ctx context.Context, kcli client.Client, flags JoinCmdFlags,
625625

626626
return addons.EnableHA(
627627
ctx,
628+
logrus.Debugf,
628629
kcli,
629630
kclient,
630631
hcli,

cmd/installer/cli/restore.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,10 @@ func runRestoreStepNew(ctx context.Context, name string, flags InstallCmdFlags,
434434
// TODO (@salah): update installation status to reflect what's happening
435435

436436
logrus.Debugf("installing addons")
437-
if err := addons.Install(ctx, hcli, addons.InstallOptions{
437+
if err := addons.Install(ctx, logrus.Debugf, hcli, addons.InstallOptions{
438438
IsAirgap: flags.airgapBundle != "",
439439
Proxy: flags.proxy,
440440
HostCABundlePath: runtimeconfig.HostCABundlePath(),
441-
PrivateCAs: flags.privateCAs,
442441
ServiceCIDR: flags.cidrCfg.ServiceCIDR,
443442
IsRestore: true,
444443
EmbeddedConfigSpec: embCfgSpec,
@@ -580,7 +579,7 @@ func runRestoreEnableAdminConsoleHA(ctx context.Context, flags InstallCmdFlags,
580579
}
581580
defer hcli.Close()
582581

583-
err = addons.EnableAdminConsoleHA(ctx, kcli, hcli, flags.isAirgap, flags.cidrCfg.ServiceCIDR, flags.proxy, in.Spec.Config, in.Spec.LicenseInfo)
582+
err = addons.EnableAdminConsoleHA(ctx, logrus.Debugf, kcli, hcli, flags.isAirgap, flags.cidrCfg.ServiceCIDR, flags.proxy, in.Spec.Config, in.Spec.LicenseInfo)
584583
if err != nil {
585584
return err
586585
}

e2e/proxy_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ func TestInstallWithMITMProxy(t *testing.T) {
337337
installSingleNodeWithOptions(t, tc, installOptions{
338338
httpProxy: lxd.HTTPMITMProxy,
339339
httpsProxy: lxd.HTTPMITMProxy,
340-
privateCA: "/usr/local/share/ca-certificates/proxy/ca.crt",
341340
withEnv: lxd.WithMITMProxyEnv(tc.IPs),
342341
})
343342

e2e/shared.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ type installOptions struct {
3232
httpProxy string
3333
httpsProxy string
3434
noProxy string
35-
privateCA string
3635
configValuesFile string
3736
networkInterface string
3837
dataDir string
@@ -116,9 +115,6 @@ func installSingleNodeWithOptions(t *testing.T, tc cluster.Cluster, opts install
116115
if opts.noProxy != "" {
117116
line = append(line, "--no-proxy", opts.noProxy)
118117
}
119-
if opts.privateCA != "" {
120-
line = append(line, "--private-ca", opts.privateCA)
121-
}
122118
if opts.configValuesFile != "" {
123119
line = append(line, "--config-values", opts.configValuesFile)
124120
}

0 commit comments

Comments
 (0)