Skip to content

Commit

Permalink
feat(akamai): unzip user content
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhem committed Sep 6, 2024
1 parent 3b3a27b commit 0d03da3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
12 changes: 9 additions & 3 deletions internal/providers/akamai/akamai.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ func fetchConfig(f *resource.Fetcher) (types.Config, report.Report, error) {
// The Linode Metadata Service requires userdata to be base64-encoded
// when it is uploaded, so we will have to decode the response.
data := make([]byte, base64.StdEncoding.DecodedLen(len(encoded)))
n, err := base64.StdEncoding.Decode(data, encoded)
if err != nil {
if _, err := base64.StdEncoding.Decode(data, encoded); err != nil {
return types.Config{}, report.Report{}, fmt.Errorf("decode base64: %w", err)
}

return util.ParseConfig(f.Logger, data[:n])
// The Linode Metadata Service can compress userdata.
// We have to gunzip if needed.
unzipData, err := util.GunzipIfNeeded(data)
if err != nil {
return types.Config{}, report.Report{}, fmt.Errorf("unzip: %w", err)
}

return util.ParseConfig(f.Logger, unzipData)
}

// defaultTokenTTL is the time-to-live (TTL; in seconds) for an authorization
Expand Down
41 changes: 41 additions & 0 deletions internal/providers/util/unzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package util

import (
"bytes"
"compress/gzip"
"io"
)

var (
gzipMagic = []byte{0x1F, 0x8B, 0x08}
)

func hasGzipMagicNumber(source []byte) bool {
return bytes.HasPrefix(source, gzipMagic)
}

func GunzipIfNeeded(raw []byte) ([]byte, error) {
if hasGzipMagicNumber(raw) {
reader, err := gzip.NewReader(bytes.NewReader(raw))
if err != nil {
return nil, err
}
defer reader.Close()
return io.ReadAll(reader)
}
return raw, nil
}

0 comments on commit 0d03da3

Please sign in to comment.