diff --git a/cmd/generate.go b/cmd/generate.go index 82a962b..7b2a38e 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -7,9 +7,10 @@ import ( "os" "path/filepath" - "github.com/Skarlso/crd-to-sample-yaml/pkg" "github.com/spf13/cobra" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" + + "github.com/Skarlso/crd-to-sample-yaml/pkg" ) const ( @@ -21,6 +22,9 @@ type rootArgs struct { fileLocation string folderLocation string url string + username string + password string + token string output string format string stdOut bool @@ -50,6 +54,9 @@ func init() { f.StringVarP(&args.fileLocation, "crd", "c", "", "The CRD file to generate a yaml from.") f.StringVarP(&args.folderLocation, "folder", "r", "", "A folder from which to parse a series of CRDs.") f.StringVarP(&args.url, "url", "u", "", "If provided, will use this URL to fetch CRD YAML content from.") + f.StringVar(&args.username, "username", "", "Optional username to authenticate a URL.") + f.StringVar(&args.password, "password", "", "Optional password to authenticate a URL.") + f.StringVar(&args.token, "token", "", "A bearer token to authenticate a URL.") f.StringVarP(&args.output, "output", "o", "", "The location of the output file. Default is next to the CRD.") f.StringVarP(&args.format, "format", "f", FormatYAML, "The format in which to output. Default is YAML. Options are: yaml, html.") f.BoolVarP(&args.stdOut, "stdout", "s", false, "If set, it will output the generated content to stdout.") @@ -124,7 +131,12 @@ func constructHandler(args *rootArgs) (Handler, error) { case args.folderLocation != "": crdHandler = &FolderHandler{location: args.folderLocation} case args.url != "": - crdHandler = &URLHandler{url: args.url} + crdHandler = &URLHandler{ + url: args.url, + username: args.username, + password: args.password, + token: args.token, + } } if crdHandler == nil { diff --git a/cmd/url_handler.go b/cmd/url_handler.go index 8ce1352..74597cb 100644 --- a/cmd/url_handler.go +++ b/cmd/url_handler.go @@ -15,14 +15,17 @@ import ( const timeout = 10 type URLHandler struct { - url string + url string + username string + password string + token string } func (h *URLHandler) CRDs() ([]*v1beta1.CustomResourceDefinition, error) { client := http.DefaultClient client.Timeout = timeout * time.Second - f := fetcher.NewFetcher(client) + f := fetcher.NewFetcher(client, h.username, h.password, h.token) content, err := f.Fetch(h.url) if err != nil { return nil, fmt.Errorf("failed to fetch content: %w", err) diff --git a/pkg/fetcher/fetch_content.go b/pkg/fetcher/fetch_content.go index d5390f1..88aec27 100644 --- a/pkg/fetcher/fetch_content.go +++ b/pkg/fetcher/fetch_content.go @@ -9,13 +9,19 @@ import ( // Fetcher wraps an HTTP client. type Fetcher struct { - client *http.Client + client *http.Client + username string + password string + token string } // NewFetcher constructs a new client wrapper with a given client. -func NewFetcher(client *http.Client) *Fetcher { +func NewFetcher(client *http.Client, username, password, token string) *Fetcher { return &Fetcher{ - client: client, + client: client, + username: username, + password: password, + token: token, } } @@ -26,6 +32,14 @@ func (f *Fetcher) Fetch(url string) ([]byte, error) { return nil, fmt.Errorf("failed to generate request for url '%s': %w", url, err) } + if f.username != "" && f.password != "" { + req.SetBasicAuth(f.username, f.password) + } + + if f.token != "" { + req.Header.Add("Authorization", "Bearer "+f.token) + } + resp, err := f.client.Do(req) if err != nil { return nil, fmt.Errorf("failed to fetch data: %w", err) diff --git a/wasm/app.go b/wasm/app.go index 54423a4..7615a7a 100644 --- a/wasm/app.go +++ b/wasm/app.go @@ -10,13 +10,13 @@ import ( "strconv" "strings" - "github.com/Skarlso/crd-to-sample-yaml/pkg/fetcher" - "github.com/Skarlso/crd-to-sample-yaml/pkg/sanitize" "github.com/maxence-charriere/go-app/v10/pkg/app" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" "k8s.io/apimachinery/pkg/util/yaml" "github.com/Skarlso/crd-to-sample-yaml/pkg" + "github.com/Skarlso/crd-to-sample-yaml/pkg/fetcher" + "github.com/Skarlso/crd-to-sample-yaml/pkg/sanitize" ) // crdView is the main component to display a rendered CRD. @@ -81,7 +81,8 @@ func (h *crdView) OnNav(ctx app.Context) { return } - f := fetcher.NewFetcher(http.DefaultClient) + // authentication is not available here. + f := fetcher.NewFetcher(http.DefaultClient, "", "", "") content, err := f.Fetch(u) if err != nil { h.preRenderErr = err diff --git a/wasm/index.go b/wasm/index.go index 57b6309..9428373 100644 --- a/wasm/index.go +++ b/wasm/index.go @@ -5,10 +5,10 @@ import ( "fmt" "net/http" - "github.com/Skarlso/crd-to-sample-yaml/pkg/sanitize" "github.com/maxence-charriere/go-app/v10/pkg/app" "github.com/Skarlso/crd-to-sample-yaml/pkg/fetcher" + "github.com/Skarlso/crd-to-sample-yaml/pkg/sanitize" ) const maximumBytes = 200 * 1000 // 200KB @@ -94,6 +94,9 @@ func (i *input) Render() app.UI { Class("url_to_crd").Class("form-control").Placeholder("Paste URL to CRD here..."). ID("url_to_crd"). Name("url_to_crd"), + app.Input().Class("url_username").Class("form-control").Placeholder("Optional username here...").ID("url_username"), + app.Input().Class("url_password").Class("form-control").Placeholder("Optional password here...").ID("url_password").Type("password"), + app.Input().Class("url_token").Class("form-control").Placeholder("Optional token here...").ID("url_token").Type("password"), ) } @@ -136,13 +139,18 @@ func (i *index) OnClick(_ app.Context, _ app.Event) { return } - f := fetcher.NewFetcher(http.DefaultClient) + username := app.Window().GetElementByID("url_username").Get("value") + password := app.Window().GetElementByID("url_password").Get("value") + token := app.Window().GetElementByID("url_token").Get("value") + + f := fetcher.NewFetcher(http.DefaultClient, username.String(), password.String(), token.String()) content, err := f.Fetch(inp.String()) if err != nil { i.err = fmt.Errorf("failed to fetch CRD content: %w", err) return } + if len(content) > maximumBytes { i.err = errors.New("content exceeds maximum length of 200KB") diff --git a/wasm/index.html b/wasm/index.html index 8683172..6be09c0 100644 --- a/wasm/index.html +++ b/wasm/index.html @@ -3,30 +3,30 @@ - + - + Preview CRDs - - - + + + - + - + - - + + - + @@ -64,7 +64,7 @@
\ No newline at end of file diff --git a/wasm/share.html b/wasm/share.html index e055645..472df5c 100644 --- a/wasm/share.html +++ b/wasm/share.html @@ -1,31 +1,31 @@ - + - + - + Preview CRDs - + - + - - - - + + + + diff --git a/wasm/web/app.wasm b/wasm/web/app.wasm index a270f2c..6b55d79 100755 Binary files a/wasm/web/app.wasm and b/wasm/web/app.wasm differ