-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Frank Jogeleit <[email protected]>
- Loading branch information
Frank Jogeleit
committed
Dec 26, 2023
1 parent
5774ce3
commit 05cc9e7
Showing
47 changed files
with
655 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/tls" | ||
"encoding/json" | ||
"io" | ||
"net" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type BasicAuth struct { | ||
Username string | ||
Password string | ||
} | ||
|
||
type Client struct { | ||
baseURL string | ||
http *http.Client | ||
auth *BasicAuth | ||
} | ||
|
||
func (c *Client) ResolveNamespaceSelector(ctx context.Context, selector map[string]string) ([]string, error) { | ||
resp, err := c.post(ctx, "/v2/namespaces/resolve-selector", selector) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
return decodeList[string](resp.Body) | ||
} | ||
|
||
// CreateJSONRequest for the given configuration | ||
func (c *Client) post(ctx context.Context, path string, payload interface{}) (*http.Response, error) { | ||
body := new(bytes.Buffer) | ||
|
||
if err := json.NewEncoder(body).Encode(payload); err != nil { | ||
return nil, err | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+path, body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if c.auth != nil { | ||
req.SetBasicAuth(c.auth.Username, c.auth.Password) | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json; charset=utf-8") | ||
req.Header.Set("User-Agent", "Policy Reporter UI") | ||
|
||
return c.http.Do(req) | ||
} | ||
|
||
func decodeList[T any](r io.Reader) ([]T, error) { | ||
list := make([]T, 0) | ||
err := json.NewDecoder(r).Decode(&list) | ||
|
||
return list, err | ||
} | ||
|
||
func New(options []ClientOption) (*Client, error) { | ||
client := &Client{ | ||
http: newHTTPClient(), | ||
} | ||
|
||
for _, o := range options { | ||
if err := o(client); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return client, nil | ||
} | ||
|
||
func newHTTPClient() *http.Client { | ||
return &http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: (&net.Dialer{ | ||
Timeout: 10 * time.Second, | ||
KeepAlive: 60 * time.Second, | ||
}).DialContext, | ||
MaxIdleConns: 100, | ||
IdleConnTimeout: 90 * time.Second, | ||
TLSHandshakeTimeout: 10 * time.Second, | ||
ExpectContinueTimeout: 1 * time.Second, | ||
TLSClientConfig: &tls.Config{}, | ||
}, | ||
Timeout: 10 * time.Second, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httputil" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
func newLoggingRoundTripper(roundTripper http.RoundTripper) http.RoundTripper { | ||
return &logRoundTripper{roundTripper: roundTripper} | ||
} | ||
|
||
type logRoundTripper struct { | ||
roundTripper http.RoundTripper | ||
} | ||
|
||
func (rt *logRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
logger := zap.L() | ||
if logger.Core().Enabled(zap.DebugLevel) { | ||
if info, err := httputil.DumpRequest(req, true); err == nil { | ||
logger.Debug(fmt.Sprintf("Sending request: %s", string(info))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
resp, err := rt.roundTripper.RoundTrip(req) | ||
if resp != nil { | ||
if logger.Core().Enabled(zap.DebugLevel) { | ||
if info, err := httputil.DumpResponse(resp, true); err == nil { | ||
logger.Debug(fmt.Sprintf("Received response: %s", string(info))) | ||
} | ||
} | ||
} | ||
return resp, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/kyverno/policy-reporter-ui/pkg/core/utils" | ||
) | ||
|
||
type ClientOption = func(*Client) error | ||
|
||
func WithBaseURL(url string) ClientOption { | ||
return func(client *Client) error { | ||
client.baseURL = url | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func WithBaseAuth(auth BasicAuth) ClientOption { | ||
return func(client *Client) error { | ||
client.auth = &auth | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func WithCertificate(path string) ClientOption { | ||
return func(client *Client) error { | ||
certs, err := utils.LoadCerts(path) | ||
if err != nil { | ||
return fmt.Errorf("with certificate failed: %w", err) | ||
} | ||
|
||
client.http.Transport.(*http.Transport).TLSClientConfig.RootCAs = certs | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func WithSkipTLS() ClientOption { | ||
return func(client *Client) error { | ||
client.http.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify = true | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func WithLogging() ClientOption { | ||
return func(client *Client) error { | ||
client.http.Transport = newLoggingRoundTripper(client.http.Transport) | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package utils | ||
|
||
import ( | ||
"crypto/x509" | ||
"os" | ||
) | ||
|
||
func LoadCerts(path string) (*x509.CertPool, error) { | ||
caCert, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
caCertPool := x509.NewCertPool() | ||
caCertPool.AppendCertsFromPEM(caCert) | ||
|
||
return caCertPool, nil | ||
} |
Oops, something went wrong.