-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nodeadm(feat): add mime multi-part support to file config provider (#…
- Loading branch information
Showing
15 changed files
with
282 additions
and
157 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package configprovider | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
func decodeIfBase64(data []byte) ([]byte, error) { | ||
e := base64.StdEncoding | ||
maxDecodedLen := e.DecodedLen(len(data)) | ||
decodedData := make([]byte, maxDecodedLen) | ||
decodedLen, err := e.Decode(decodedData, data) | ||
if err != nil { | ||
return data, nil | ||
} | ||
return decodedData[:decodedLen], nil | ||
} | ||
|
||
// https://en.wikipedia.org/wiki/Gzip | ||
const gzipMagicNumber = uint16(0x1f8b) | ||
|
||
func decompressIfGZIP(data []byte) ([]byte, error) { | ||
if len(data) < 2 { | ||
return data, nil | ||
} | ||
preamble := uint16(data[0])<<8 | uint16(data[1]) | ||
if preamble == gzipMagicNumber { | ||
reader, err := gzip.NewReader(bytes.NewReader(data)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create GZIP reader: %v", err) | ||
} | ||
if decompressed, err := io.ReadAll(reader); err != nil { | ||
return nil, fmt.Errorf("failed to read from GZIP reader: %v", err) | ||
} else { | ||
return decompressed, nil | ||
} | ||
} | ||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package configprovider | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"mime" | ||
"mime/multipart" | ||
"net/mail" | ||
"strings" | ||
|
||
internalapi "github.com/awslabs/amazon-eks-ami/nodeadm/internal/api" | ||
apibridge "github.com/awslabs/amazon-eks-ami/nodeadm/internal/api/bridge" | ||
) | ||
|
||
func parseMaybeMultipart(data []byte) (*internalapi.NodeConfig, error) { | ||
// if the MIME data fails to parse as a multipart document, then fall back | ||
// to parsing the entire userdata as the node config. | ||
if multipartReader, err := getMultipartReader(data); err == nil { | ||
config, err := parseMultipart(multipartReader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return config, nil | ||
} else { | ||
config, err := apibridge.DecodeNodeConfig(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return config, nil | ||
} | ||
} | ||
|
||
func parseMultipart(userDataReader *multipart.Reader) (*internalapi.NodeConfig, error) { | ||
var nodeConfigs []*internalapi.NodeConfig | ||
for { | ||
part, err := userDataReader.NextPart() | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
if partHeader := part.Header.Get(contentTypeHeader); len(partHeader) > 0 { | ||
mediaType, _, err := mime.ParseMediaType(partHeader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if mediaType == nodeConfigMediaType { | ||
nodeConfigPart, err := io.ReadAll(part) | ||
if err != nil { | ||
return nil, err | ||
} | ||
nodeConfigPart, err = decodeIfBase64(nodeConfigPart) | ||
if err != nil { | ||
return nil, err | ||
} | ||
nodeConfigPart, err = decompressIfGZIP(nodeConfigPart) | ||
if err != nil { | ||
return nil, err | ||
} | ||
decodedConfig, err := apibridge.DecodeNodeConfig(nodeConfigPart) | ||
if err != nil { | ||
return nil, err | ||
} | ||
nodeConfigs = append(nodeConfigs, decodedConfig) | ||
} | ||
} | ||
} | ||
if len(nodeConfigs) > 0 { | ||
var config = nodeConfigs[0] | ||
for _, nodeConfig := range nodeConfigs[1:] { | ||
if err := config.Merge(nodeConfig); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return config, nil | ||
} else { | ||
return nil, fmt.Errorf("could not find NodeConfig within UserData") | ||
} | ||
} | ||
|
||
func getMultipartReader(data []byte) (*multipart.Reader, error) { | ||
msg, err := mail.ReadMessage(bytes.NewReader(data)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
mediaType, params, err := mime.ParseMediaType(msg.Header.Get(contentTypeHeader)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !strings.HasPrefix(mediaType, multipartContentTypePrefix) { | ||
return nil, fmt.Errorf("MIME type is not multipart") | ||
} | ||
return multipart.NewReader(msg.Body, params[mimeBoundaryParam]), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.