Skip to content

Commit 14fa8cc

Browse files
committed
CORS-3417: capi/aws: support ignition proxy and CA bundle
Make sure ignition configuration respects proxy/noproxy and CA bundle settings.
1 parent 1df0049 commit 14fa8cc

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

pkg/asset/machines/aws/awsmachines.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
package aws
33

44
import (
5+
"bytes"
6+
"encoding/pem"
57
"fmt"
8+
"strings"
69

10+
"github.com/vincent-petithory/dataurl"
711
v1 "k8s.io/api/core/v1"
812
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
913
"k8s.io/apimachinery/pkg/util/sets"
@@ -24,6 +28,7 @@ type MachineInput struct {
2428
Subnets map[string]string
2529
Tags capa.Tags
2630
PublicIP bool
31+
Ignition *capa.Ignition
2732
}
2833

2934
// GenerateMachines returns manifests and runtime objects to provision the control plane (including bootstrap, if applicable) nodes using CAPI.
@@ -70,7 +75,7 @@ func GenerateMachines(clusterID string, in *MachineInput) ([]*asset.RuntimeFile,
7075
},
7176
},
7277
Spec: capa.AWSMachineSpec{
73-
Ignition: &capa.Ignition{Version: "3.2"},
78+
Ignition: in.Ignition,
7479
UncompressedUserData: ptr.To(true),
7580
InstanceType: mpool.InstanceType,
7681
AMI: capa.AMIReference{ID: ptr.To(mpool.AMIID)},
@@ -160,3 +165,62 @@ func CapaTagsFromUserTags(clusterID string, usertags map[string]string) (capa.Ta
160165
}
161166
return tags, nil
162167
}
168+
169+
// CapaIgnitionWithCertBundleAndProxy generates CAPA ignition config with cert and proxy information.
170+
func CapaIgnitionWithCertBundleAndProxy(userCA string, proxy *types.Proxy) (*capa.Ignition, error) {
171+
carefs, err := parseCertificateBundle([]byte(userCA))
172+
if err != nil {
173+
return nil, err
174+
}
175+
return &capa.Ignition{
176+
Version: "3.2",
177+
TLS: &capa.IgnitionTLS{
178+
CASources: carefs,
179+
},
180+
Proxy: capaIgnitionProxy(proxy),
181+
}, nil
182+
}
183+
184+
// TODO: try to share this code with ignition.bootstrap package?
185+
// parseCertificateBundle loads each certificate in the bundle to the CAPA
186+
// carrier type, ignoring any invisible character before, after and in between
187+
// certificates.
188+
func parseCertificateBundle(userCA []byte) ([]capa.IgnitionCASource, error) {
189+
userCA = bytes.TrimSpace(userCA)
190+
191+
var carefs []capa.IgnitionCASource
192+
for len(userCA) > 0 {
193+
var block *pem.Block
194+
block, userCA = pem.Decode(userCA)
195+
if block == nil {
196+
return nil, fmt.Errorf("unable to parse certificate, please check the certificates")
197+
}
198+
199+
carefs = append(carefs, capa.IgnitionCASource(dataurl.EncodeBytes(pem.EncodeToMemory(block))))
200+
201+
userCA = bytes.TrimSpace(userCA)
202+
}
203+
204+
return carefs, nil
205+
}
206+
207+
func capaIgnitionProxy(proxy *types.Proxy) *capa.IgnitionProxy {
208+
capaProxy := &capa.IgnitionProxy{}
209+
if proxy == nil {
210+
return capaProxy
211+
}
212+
if httpProxy := proxy.HTTPProxy; httpProxy != "" {
213+
capaProxy.HTTPProxy = &httpProxy
214+
}
215+
if httpsProxy := proxy.HTTPSProxy; httpsProxy != "" {
216+
capaProxy.HTTPSProxy = &httpsProxy
217+
}
218+
capaProxy.NoProxy = make([]capa.IgnitionNoProxy, 0, len(proxy.NoProxy))
219+
if noProxy := proxy.NoProxy; noProxy != "" {
220+
noProxySplit := strings.Split(noProxy, ",")
221+
for _, p := range noProxySplit {
222+
capaProxy.NoProxy = append(capaProxy.NoProxy, capa.IgnitionNoProxy(p))
223+
}
224+
}
225+
return capaProxy
226+
}

pkg/asset/machines/clusterapi.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,19 @@ func (c *ClusterAPI) Generate(dependencies asset.Parents) error {
147147
return fmt.Errorf("failed to create CAPA tags from UserTags: %w", err)
148148
}
149149

150+
ignition, err := aws.CapaIgnitionWithCertBundleAndProxy(installConfig.Config.AdditionalTrustBundle, installConfig.Config.Proxy)
151+
if err != nil {
152+
return fmt.Errorf("failed to generation CAPA ignition: %w", err)
153+
}
154+
150155
pool.Platform.AWS = &mpool
151156
awsMachines, err := aws.GenerateMachines(clusterID.InfraID, &aws.MachineInput{
152157
Role: "master",
153158
Pool: &pool,
154159
Subnets: subnets,
155160
Tags: tags,
156161
PublicIP: false,
162+
Ignition: ignition,
157163
})
158164
if err != nil {
159165
return errors.Wrap(err, "failed to create master machine objects")
@@ -170,6 +176,7 @@ func (c *ClusterAPI) Generate(dependencies asset.Parents) error {
170176
Pool: &pool,
171177
Tags: tags,
172178
PublicIP: installConfig.Config.Publish == types.ExternalPublishingStrategy,
179+
Ignition: ignition,
173180
})
174181
if err != nil {
175182
return fmt.Errorf("failed to create bootstrap machine object: %w", err)

0 commit comments

Comments
 (0)