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

Added support for IAP to roer #58

Open
wants to merge 2 commits into
base: master
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
4 changes: 4 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ func NewRoer(version string, clientConfig spinnaker.ClientConfig) *cli.App {
Name: "apiSession, as",
Usage: "your active api session",
},
cli.StringFlag{
Name: "iapToken, iap",
Usage: "your IAP bearer token",
},
cli.BoolFlag{
Name: "insecure",
Usage: "Bypass TLS certificate validation",
Expand Down
4 changes: 2 additions & 2 deletions spinnaker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (c *client) DeleteTemplate(templateID string) (*TaskRefResponse, error) {
}

func (c *client) GetTask(refURL string) (*ExecutionResponse, error) {
resp, err := c.httpClient.Get(c.endpoint + refURL)
resp, err := c.Get(c.endpoint + refURL)

if err != nil {
return nil, errors.Wrap(err, "getting task status")
Expand Down Expand Up @@ -359,7 +359,7 @@ func (c *client) PollTaskStatus(refURL string, timeout time.Duration) (*Executio
func (c *client) GetPipelineConfig(app, pipelineConfigID string) (*PipelineConfig, error) {
url := c.pipelineConfigURL(app, pipelineConfigID)
logrus.WithField("url", url).Debug("getting url")
resp, err := c.httpClient.Get(url)
resp, err := c.Get(url)

if err != nil {
return nil, errors.Wrap(err, "getting pipeline config")
Expand Down
56 changes: 52 additions & 4 deletions spinnaker/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"net/url"
"os"
"time"
"io"
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -20,6 +22,9 @@ import (
// HTTPClientFactory creates a new http.Client from the cli.Context
type HTTPClientFactory func(cc *cli.Context) (*http.Client, error)

// Making iapToken a global variable to be accessed from http methods below if exists.
var iapToken string

// DefaultHTTPClientFactory creates a basic http.Client that by default can
// take an x509 cert/key pair for API authentication.
func DefaultHTTPClientFactory(cc *cli.Context) (*http.Client, error) {
Expand Down Expand Up @@ -48,6 +53,7 @@ func DefaultHTTPClientFactory(cc *cli.Context) (*http.Client, error) {
var certPath string
var keyPath string


if cc.GlobalIsSet("certPath") {
certPath = cc.GlobalString("certPath")
} else if os.Getenv("SPINNAKER_CLIENT_CERT") != "" {
Expand All @@ -62,7 +68,13 @@ func DefaultHTTPClientFactory(cc *cli.Context) (*http.Client, error) {
} else {
keyPath = ""
}

if cc.GlobalIsSet("iapToken") {
iapToken = cc.GlobalString("iapToken")
} else if os.Getenv("SPINNAKER_IAP_TOKEN") != "" {
iapToken = os.Getenv("SPINNAKER_IAP_TOKEN")
} else {
iapToken = ""
}
c.Transport = &http.Transport{
TLSClientConfig: &tls.Config{},
}
Expand Down Expand Up @@ -100,7 +112,7 @@ func (c *client) postJSON(url string, body interface{}) (resp *http.Response, re
if err != nil {
return nil, nil, errors.Wrap(err, "marshaling body to json")
}
resp, err = c.httpClient.Post(url, "application/json", bytes.NewBuffer(payload))
resp, err = c.Post(url, "application/json", bytes.NewBuffer(payload))
if err != nil {
return nil, nil, errors.Wrapf(err, "posting to %s", url)
}
Expand Down Expand Up @@ -140,7 +152,7 @@ func (c *client) postForm(url string, data url.Values) (resp *http.Response, res
}

func (c *client) getJSON(url string) (resp *http.Response, respBody []byte, err error) {
resp, err = c.httpClient.Get(url)
resp, err = c.Get(url)
if err != nil {
return nil, nil, errors.Wrapf(err, "posting to %s", url)
}
Expand All @@ -165,7 +177,7 @@ func (c *client) delete(url string) (resp *http.Response, respBody []byte, err e
return nil, nil, errors.Wrap(err, "failed to create delete request object")
}

resp, err = c.httpClient.Do(req)
resp, err = c.Do(req)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to make delete request to %s", url)
}
Expand All @@ -183,3 +195,39 @@ func (c *client) delete(url string) (resp *http.Response, respBody []byte, err e

return resp, respBody, nil
}

func (c *client) Get(url string) (resp *http.Response, err error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err

}

return c.Do(req)

}

func (c *client) PostForm(url string, data url.Values) (resp *http.Response, err error) {
return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
}

func (c *client) Post(url, contentType string, body io.Reader) (resp *http.Response, err error) {
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err

}

req.Header.Set("Content-Type", contentType)

return c.Do(req)

}

func (c *client) Do(req *http.Request) (*http.Response, error) {
if iapToken != "" {
req.Header.Add("Authorization", "Bearer " + iapToken)
}
return c.httpClient.Do(req)
}