Skip to content

Commit

Permalink
feat: add url authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso committed Jul 3, 2024
1 parent 4caa4b7 commit 4474c9d
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 32 deletions.
16 changes: 14 additions & 2 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions cmd/url_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 17 additions & 3 deletions pkg/fetcher/fetch_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions wasm/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions wasm/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
)
}

Expand Down Expand Up @@ -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")

Expand Down
22 changes: 11 additions & 11 deletions wasm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
<head>
<meta charset="UTF-8">
<meta name="author" content="Gergely Brautigam">
<meta name="description" content>
<meta content name="description">
<meta name="theme-color" content="#2d2c2c">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0, viewport-fit=cover">
<meta property="og:url" content="https://">
<meta property="og:title" content="Preview CRDs">
<meta property="og:description" content>
<meta property="og:type" content="website">
<meta property="og:image" content="https://">
<meta content="https://" property="og:image">
<title>Preview CRDs</title>
<link href="/app.css" type="text/css" rel="preload" as="style">
<link href="/web/css/alert.css" type="text/css" rel="preload" as="style">
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-twilight.min.css" type="text/css" rel="preload" as="style">
<link rel="preload" as="style" href="/app.css" type="text/css">
<link type="text/css" rel="preload" as="style" href="/web/css/alert.css">
<link as="style" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-twilight.min.css" type="text/css" rel="preload">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/css/halfmoon.min.css" type="text/css" rel="preload" as="style">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/css/cores/halfmoon.modern.css" type="text/css" rel="preload" as="style">
<link as="style" href="https://cdn.jsdelivr.net/npm/[email protected]/css/cores/halfmoon.modern.css" type="text/css" rel="preload">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css" rel="preload" as="style">
<link rel="icon" href="https://raw.githubusercontent.com/maxence-charriere/go-app/master/docs/web/icon.svg">
<link rel="apple-touch-icon" href="/web/img/logo.png">
<link rel="manifest" href="/manifest.webmanifest">
<link href="/app.css" type="text/css" rel="stylesheet">
<link rel="stylesheet" href="/app.css" type="text/css">
<link href="/web/css/alert.css" type="text/css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-twilight.min.css" type="text/css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/css/halfmoon.min.css" type="text/css" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-twilight.min.css">
<link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/halfmoon.min.css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/css/cores/halfmoon.modern.css" type="text/css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script defer src="/wasm_exec.js"></script>
<script defer src="/app.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
Expand Down Expand Up @@ -64,7 +64,7 @@
<main></main>
<aside id="app-wasm-loader" class="goapp-app-info">
<img class="goapp-logo goapp-spin" alt="wasm loader icon" src="/web/img/logo.png" id="app-wasm-loader-icon">
<p class="goapp-label" id="app-wasm-loader-label">0%</p>
<p id="app-wasm-loader-label" class="goapp-label">0%</p>
</aside>
</body>
</html>
18 changes: 9 additions & 9 deletions wasm/share.html
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
<!DOCTYPE html>
<html data-bs-core="modern" data-bs-theme="dark" lang="en">
<html lang="en" data-bs-core="modern" data-bs-theme="dark">
<head>
<meta charset="UTF-8">
<meta name="author" content="Gergely Brautigam">
<meta name="description" content>
<meta name="theme-color" content="#2d2c2c">
<meta content="#2d2c2c" name="theme-color">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0, viewport-fit=cover">
<meta content="https:///share" property="og:url">
<meta property="og:url" content="https:///share">
<meta property="og:title" content="Preview CRDs">
<meta property="og:description" content>
<meta property="og:type" content="website">
<meta property="og:image" content="https://">
<title>Preview CRDs</title>
<link href="/app.css" type="text/css" rel="preload" as="style">
<link type="text/css" rel="preload" as="style" href="/web/css/alert.css">
<link href="/web/css/alert.css" type="text/css" rel="preload" as="style">
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-twilight.min.css" type="text/css" rel="preload" as="style">
<link rel="preload" as="style" href="https://cdn.jsdelivr.net/npm/[email protected]/css/halfmoon.min.css" type="text/css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/css/cores/halfmoon.modern.css" type="text/css" rel="preload" as="style">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css" rel="preload" as="style">
<link rel="icon" href="https://raw.githubusercontent.com/maxence-charriere/go-app/master/docs/web/icon.svg">
<link rel="apple-touch-icon" href="/web/img/logo.png">
<link href="/manifest.webmanifest" rel="manifest">
<link rel="manifest" href="/manifest.webmanifest">
<link href="/app.css" type="text/css" rel="stylesheet">
<link href="/web/css/alert.css" type="text/css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-twilight.min.css" type="text/css" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/halfmoon.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/cores/halfmoon.modern.css" type="text/css">
<link rel="stylesheet" href="/web/css/alert.css" type="text/css">
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-twilight.min.css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/css/halfmoon.min.css" type="text/css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/css/cores/halfmoon.modern.css" type="text/css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css" rel="stylesheet">
<script defer src="/wasm_exec.js"></script>
<script defer src="/app.js"></script>
Expand Down
Binary file modified wasm/web/app.wasm
Binary file not shown.

0 comments on commit 4474c9d

Please sign in to comment.