Skip to content

Commit

Permalink
Update p2p boostrap helpers for Spegel v0.0.30
Browse files Browse the repository at this point in the history
Signed-off-by: Brad Davidson <[email protected]>
  • Loading branch information
brandond committed Jan 22, 2025
1 parent 4474c0a commit 306ab8b
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 92 deletions.
56 changes: 44 additions & 12 deletions pkg/clientaccess/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ var (
// ClientOption is a callback to mutate the http client prior to use
type ClientOption func(*http.Client)

// RequestOption is a callback to mutate the http request prior to use
type RequestOption func(*http.Request)

// Info contains fields that track parsed parts of a cluster join token
type Info struct {
*kubeadm.BootstrapTokenString
Expand Down Expand Up @@ -240,7 +243,7 @@ func parseToken(token string) (*Info, error) {
// If the CA bundle is not empty but does not contain any valid certs, it validates using
// an empty CA bundle (which will always fail).
// If valid cert+key paths can be loaded from the provided paths, they are used for client cert auth.
func GetHTTPClient(cacerts []byte, certFile, keyFile string, option ...ClientOption) *http.Client {
func GetHTTPClient(cacerts []byte, certFile, keyFile string, options ...any) *http.Client {
if len(cacerts) == 0 {
return defaultClient
}
Expand All @@ -265,8 +268,10 @@ func GetHTTPClient(cacerts []byte, certFile, keyFile string, option ...ClientOpt
},
}

for _, o := range option {
o(client)
for _, o := range options {
if clientOption, ok := o.(ClientOption); ok {
clientOption(client)
}
}
return client
}
Expand All @@ -278,8 +283,14 @@ func WithTimeout(d time.Duration) ClientOption {
}
}

func WithHeader(k, v string) RequestOption {
return func(r *http.Request) {
r.Header.Add(k, v)
}
}

// Get makes a request to a subpath of info's BaseURL
func (i *Info) Get(path string, option ...ClientOption) ([]byte, error) {
func (i *Info) Get(path string, options ...any) ([]byte, error) {
u, err := url.Parse(i.BaseURL)
if err != nil {
return nil, err
Expand All @@ -290,11 +301,12 @@ func (i *Info) Get(path string, option ...ClientOption) ([]byte, error) {
}
p.Scheme = u.Scheme
p.Host = u.Host
return get(p.String(), GetHTTPClient(i.CACerts, i.CertFile, i.KeyFile, option...), i.Username, i.Password, i.Token())
client := GetHTTPClient(i.CACerts, i.CertFile, i.KeyFile, options...)
return get(p.String(), client, i.Username, i.Password, i.Token(), options...)
}

// Put makes a request to a subpath of info's BaseURL
func (i *Info) Put(path string, body []byte, option ...ClientOption) error {
func (i *Info) Put(path string, body []byte, options ...any) error {
u, err := url.Parse(i.BaseURL)
if err != nil {
return err
Expand All @@ -305,11 +317,12 @@ func (i *Info) Put(path string, body []byte, option ...ClientOption) error {
}
p.Scheme = u.Scheme
p.Host = u.Host
return put(p.String(), body, GetHTTPClient(i.CACerts, i.CertFile, i.KeyFile, option...), i.Username, i.Password, i.Token())
client := GetHTTPClient(i.CACerts, i.CertFile, i.KeyFile, options...)
return put(p.String(), body, client, i.Username, i.Password, i.Token(), options...)
}

// Post makes a request to a subpath of info's BaseURL
func (i *Info) Post(path string, body []byte, option ...ClientOption) ([]byte, error) {
func (i *Info) Post(path string, body []byte, options ...any) ([]byte, error) {
u, err := url.Parse(i.BaseURL)
if err != nil {
return nil, err
Expand All @@ -320,7 +333,8 @@ func (i *Info) Post(path string, body []byte, option ...ClientOption) ([]byte, e
}
p.Scheme = u.Scheme
p.Host = u.Host
return post(p.String(), body, GetHTTPClient(i.CACerts, i.CertFile, i.KeyFile, option...), i.Username, i.Password, i.Token())
client := GetHTTPClient(i.CACerts, i.CertFile, i.KeyFile, options...)
return post(p.String(), body, client, i.Username, i.Password, i.Token(), options...)
}

// setServer sets the BaseURL and CACerts fields of the Info by connecting to the server
Expand Down Expand Up @@ -402,7 +416,7 @@ func getCACerts(u url.URL) ([]byte, error) {

// get makes a request to a url using a provided client and credentials,
// returning the response body.
func get(u string, client *http.Client, username, password, token string) ([]byte, error) {
func get(u string, client *http.Client, username, password, token string, options ...any) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
Expand All @@ -414,6 +428,12 @@ func get(u string, client *http.Client, username, password, token string) ([]byt
req.SetBasicAuth(username, password)
}

for _, o := range options {
if requestOption, ok := o.(RequestOption); ok {
requestOption(req)
}
}

resp, err := client.Do(req)
if err != nil {
return nil, err
Expand All @@ -424,7 +444,7 @@ func get(u string, client *http.Client, username, password, token string) ([]byt

// put makes a request to a url using a provided client and credentials,
// only an error is returned
func put(u string, body []byte, client *http.Client, username, password, token string) error {
func put(u string, body []byte, client *http.Client, username, password, token string, options ...any) error {
req, err := http.NewRequest(http.MethodPut, u, bytes.NewBuffer(body))
if err != nil {
return err
Expand All @@ -436,6 +456,12 @@ func put(u string, body []byte, client *http.Client, username, password, token s
req.SetBasicAuth(username, password)
}

for _, o := range options {
if requestOption, ok := o.(RequestOption); ok {
requestOption(req)
}
}

resp, err := client.Do(req)
if err != nil {
return err
Expand All @@ -447,7 +473,7 @@ func put(u string, body []byte, client *http.Client, username, password, token s

// post makes a request to a url using a provided client and credentials,
// returning the response body and error.
func post(u string, body []byte, client *http.Client, username, password, token string) ([]byte, error) {
func post(u string, body []byte, client *http.Client, username, password, token string, options ...any) ([]byte, error) {
req, err := http.NewRequest(http.MethodPost, u, bytes.NewBuffer(body))
if err != nil {
return nil, err
Expand All @@ -459,6 +485,12 @@ func post(u string, body []byte, client *http.Client, username, password, token
req.SetBasicAuth(username, password)
}

for _, o := range options {
if requestOption, ok := o.(RequestOption); ok {
requestOption(req)
}
}

resp, err := client.Do(req)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 306ab8b

Please sign in to comment.