Skip to content

Commit be1f3e1

Browse files
committed
fetch: Add support for providing HTTP headers
I plan to use this in ignition-dracut to inject e.g. headers from `/usr/lib/os-release`, so that a server (such as the OpenShift 4 machine-config-operator server) can dispatch on them.
1 parent 3e633de commit be1f3e1

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

internal/exec/engine.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import (
2020
"encoding/json"
2121
"fmt"
2222
"io/ioutil"
23+
"net/http"
24+
"strings"
25+
2326
"net/url"
2427
"os"
2528
"time"
@@ -50,6 +53,7 @@ const (
5053
type Engine struct {
5154
ConfigCache string
5255
FetchTimeout time.Duration
56+
FetchHeaders []string
5357
Logger *log.Logger
5458
Root string
5559
PlatformConfig platform.Config
@@ -272,7 +276,17 @@ func (e *Engine) fetchReferencedConfig(cfgRef types.ConfigReference) (types.Conf
272276
if err != nil {
273277
return types.Config{}, err
274278
}
275-
rawCfg, err := e.Fetcher.FetchToBuffer(*u, resource.FetchOptions{})
279+
headers := make(http.Header)
280+
for _, h := range e.FetchHeaders {
281+
parts := strings.SplitN(h, "=", 2)
282+
k := parts[0]
283+
v := ""
284+
if len(parts) > 1 {
285+
v = parts[1]
286+
}
287+
headers.Add(k, v)
288+
}
289+
rawCfg, err := e.Fetcher.FetchToBuffer(*u, resource.FetchOptions{Headers: headers})
276290
if err != nil {
277291
return types.Config{}, err
278292
}

internal/main.go

+14
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,23 @@ import (
3232
"github.com/coreos/ignition/v2/internal/version"
3333
)
3434

35+
type flagStringArray []string
36+
37+
func (_ *flagStringArray) String() string {
38+
return ""
39+
}
40+
41+
func (a *flagStringArray) Set(value string) error {
42+
*a = append(*a, value)
43+
return nil
44+
}
45+
3546
func main() {
3647
flags := struct {
3748
clearCache bool
3849
configCache string
3950
fetchTimeout time.Duration
51+
fetchHeaders flagStringArray
4052
platform platform.Name
4153
root string
4254
stage stages.Name
@@ -47,6 +59,7 @@ func main() {
4759
flag.BoolVar(&flags.clearCache, "clear-cache", false, "clear any cached config")
4860
flag.StringVar(&flags.configCache, "config-cache", "/run/ignition.json", "where to cache the config")
4961
flag.DurationVar(&flags.fetchTimeout, "fetch-timeout", exec.DefaultFetchTimeout, "initial duration for which to wait for config")
62+
flag.Var(&flags.fetchHeaders, "fetch-header", "Include HTTP header for fetches")
5063
flag.Var(&flags.platform, "platform", fmt.Sprintf("current platform. %v", platform.Names()))
5164
flag.StringVar(&flags.root, "root", "/", "root of the filesystem")
5265
flag.Var(&flags.stage, "stage", fmt.Sprintf("execution stage. %v", stages.Names()))
@@ -91,6 +104,7 @@ func main() {
91104
engine := exec.Engine{
92105
Root: flags.root,
93106
FetchTimeout: flags.fetchTimeout,
107+
FetchHeaders: flags.fetchHeaders,
94108
Logger: &logger,
95109
ConfigCache: flags.configCache,
96110
PlatformConfig: platformConfig,

internal/resource/url.go

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ type Fetcher struct {
6868
// timeouts Ignition was configured to used will be ignored.
6969
client *HttpClient
7070

71+
// headers is additional headers to include
72+
headers []http.Header
73+
7174
// The AWS Session to use when fetching resources from S3. If left nil, the
7275
// first S3 object that is fetched will initialize the field. This can be
7376
// used to set credentials.

0 commit comments

Comments
 (0)