Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decompress GZIP'd user data #1762

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 70 additions & 5 deletions nodeadm/internal/configprovider/userdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package configprovider

import (
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
"io"
"mime"
Expand All @@ -22,17 +24,39 @@ const (
nodeConfigMediaType = "application/" + api.GroupName
)

type userDataConfigProvider struct{}
type userDataProvider interface {
GetUserData() ([]byte, error)
}
cartermckinnon marked this conversation as resolved.
Show resolved Hide resolved

type imdsUserDataProvider struct{}

func (p *imdsUserDataProvider) GetUserData() ([]byte, error) {
return imds.GetUserData()
}

type userDataConfigProvider struct {
userDataProvider userDataProvider
}
cartermckinnon marked this conversation as resolved.
Show resolved Hide resolved

func NewUserDataConfigProvider() ConfigProvider {
return &userDataConfigProvider{}
return &userDataConfigProvider{
userDataProvider: &imdsUserDataProvider{},
}
}

func (ics *userDataConfigProvider) Provide() (*internalapi.NodeConfig, error) {
userData, err := imds.GetUserData()
func (p *userDataConfigProvider) Provide() (*internalapi.NodeConfig, error) {
userData, err := p.userDataProvider.GetUserData()
if err != nil {
return nil, err
}
userData, err = decodeIfBase64(userData)
if err != nil {
return nil, fmt.Errorf("failed to decode user data: %v", err)
}
userData, err = decompressIfGZIP(userData)
if err != nil {
return nil, fmt.Errorf("failed to decompress user data: %v", err)
}
// 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 := getMIMEMultipartReader(userData); err == nil {
Expand Down Expand Up @@ -85,6 +109,14 @@ func parseMultipart(userDataReader *multipart.Reader) (*internalapi.NodeConfig,
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
Comment on lines +113 to +118
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: theres a custom error message around the calls above but not here :(

Copy link
Member

@ndbaker1 ndbaker1 May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the more i think on this, should we be more tolerant and break on these errors so that we can try the remaining parts? maybe not if theres just a simple mistake and the user would prefer it doesn't continue if their config is not included

}
decodedConfig, err := apibridge.DecodeNodeConfig(nodeConfigPart)
if err != nil {
return nil, err
Expand All @@ -102,6 +134,39 @@ func parseMultipart(userDataReader *multipart.Reader) (*internalapi.NodeConfig,
}
return config, nil
} else {
return nil, fmt.Errorf("Could not find NodeConfig within UserData")
return nil, fmt.Errorf("could not find NodeConfig within UserData")
}
}

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
Comment on lines +145 to +147
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so none of the possible errors here end up mattering?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh i guess it can only return CorruptInputError 🤔

}
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
}