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

fetch: Add support for providing HTTP headers #889

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion internal/exec/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"

"net/url"
"os"
"time"
Expand Down Expand Up @@ -50,6 +53,7 @@ const (
type Engine struct {
ConfigCache string
FetchTimeout time.Duration
FetchHeaders []string
Logger *log.Logger
Root string
PlatformConfig platform.Config
Expand Down Expand Up @@ -272,7 +276,17 @@ func (e *Engine) fetchReferencedConfig(cfgRef types.ConfigReference) (types.Conf
if err != nil {
return types.Config{}, err
}
rawCfg, err := e.Fetcher.FetchToBuffer(*u, resource.FetchOptions{})
headers := make(http.Header)
Copy link
Contributor

@ajeddeloh ajeddeloh Nov 22, 2019

Choose a reason for hiding this comment

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

This means the headers specified only happen to config fetches, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah.

for _, h := range e.FetchHeaders {
parts := strings.SplitN(h, "=", 2)
k := parts[0]
v := ""
if len(parts) > 1 {
v = parts[1]
}
headers.Add(k, v)
}
rawCfg, err := e.Fetcher.FetchToBuffer(*u, resource.FetchOptions{Headers: headers})
if err != nil {
return types.Config{}, err
}
Expand Down
14 changes: 14 additions & 0 deletions internal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,23 @@ import (
"github.com/coreos/ignition/v2/internal/version"
)

type flagStringArray []string

func (_ *flagStringArray) String() string {
return ""
}

func (a *flagStringArray) Set(value string) error {
*a = append(*a, value)
return nil
}

func main() {
flags := struct {
clearCache bool
configCache string
fetchTimeout time.Duration
fetchHeaders flagStringArray
platform platform.Name
root string
stage stages.Name
Expand All @@ -47,6 +59,7 @@ func main() {
flag.BoolVar(&flags.clearCache, "clear-cache", false, "clear any cached config")
flag.StringVar(&flags.configCache, "config-cache", "/run/ignition.json", "where to cache the config")
flag.DurationVar(&flags.fetchTimeout, "fetch-timeout", exec.DefaultFetchTimeout, "initial duration for which to wait for config")
flag.Var(&flags.fetchHeaders, "fetch-header", "Include HTTP header for fetches")
flag.Var(&flags.platform, "platform", fmt.Sprintf("current platform. %v", platform.Names()))
flag.StringVar(&flags.root, "root", "/", "root of the filesystem")
flag.Var(&flags.stage, "stage", fmt.Sprintf("execution stage. %v", stages.Names()))
Expand Down Expand Up @@ -91,6 +104,7 @@ func main() {
engine := exec.Engine{
Root: flags.root,
FetchTimeout: flags.fetchTimeout,
FetchHeaders: flags.fetchHeaders,
Logger: &logger,
ConfigCache: flags.configCache,
PlatformConfig: platformConfig,
Expand Down
3 changes: 3 additions & 0 deletions internal/resource/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ type Fetcher struct {
// timeouts Ignition was configured to used will be ignored.
client *HttpClient

// headers is additional headers to include
headers []http.Header

// The AWS Session to use when fetching resources from S3. If left nil, the
// first S3 object that is fetched will initialize the field. This can be
// used to set credentials.
Expand Down