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

Reworking the datasource format #84

Merged
merged 18 commits into from
Sep 26, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
override unmarshall for HTTPWhiteListConfig
Signed-off-by: Augustin Husson <husson.augustin@gmail.com>
  • Loading branch information
Nexucis committed Sep 26, 2021
commit 358ad6d1b18abd7a1ed4064f1283d623ad475f38
58 changes: 51 additions & 7 deletions pkg/model/api/v1/datasource/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)

Expand Down Expand Up @@ -68,11 +69,54 @@ func (h *HTTPAccess) validate() error {
return nil
}

type HTTPWhiteListConfiguration struct {
type HTTPWhiteListConfig struct {
Endpoint string `json:"endpoint" yaml:"endpoint"`
Method string `json:"method" yaml:"method"`
}

func (h *HTTPWhiteListConfig) UnmarshalJSON(data []byte) error {
var tmp HTTPWhiteListConfig
type plain HTTPWhiteListConfig
if err := json.Unmarshal(data, (*plain)(&tmp)); err != nil {
return err
}
if err := (&tmp).validate(); err != nil {
return err
}
*h = tmp
return nil
}

func (h *HTTPWhiteListConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
var tmp HTTPWhiteListConfig
type plain HTTPWhiteListConfig
if err := unmarshal((*plain)(&tmp)); err != nil {
return err
}
if err := (&tmp).validate(); err != nil {
return err
}
*h = tmp
return nil
}

func (h *HTTPWhiteListConfig) validate() error {
if len(h.Method) == 0 {
return fmt.Errorf("HTTP method cannot be empty")
}
if len(h.Endpoint) == 0 {
return fmt.Errorf("HTTP endpoint cannot be empty")
}
if h.Method != http.MethodGet &&
h.Method != http.MethodPost &&
h.Method != http.MethodDelete &&
h.Method != http.MethodPut &&
h.Method != http.MethodPatch {
return fmt.Errorf("'%s' is not a valid http method. Current supported HTTP method: %s, %s, %s, %s, %s", h.Method, http.MethodGet, http.MethodPost, http.MethodDelete, http.MethodPut, http.MethodPatch)
}
return nil
}

type BasicAuth struct {
Username string `json:"username" yaml:"username"`
Password string `json:"password" yaml:"password,omitempty"`
Expand Down Expand Up @@ -169,7 +213,7 @@ type HTTPConfiguration struct {
Access HTTPAccess `json:"access,omitempty" yaml:"access,omitempty"`
// WhiteList is a list of tuple of http method and http endpoint that will be accessible.
// These parameters are only used when access is set to 'server'
WhiteList []HTTPWhiteListConfiguration `json:"white_list" yaml:"white_list"`
WhiteList []HTTPWhiteListConfig `json:"white_list" yaml:"white_list"`
// Auth is holding any security configuration for the http configuration.
// When defined, it's impossible to set the value of Access with 'browser'
Auth *HTTPAuth `json:"auth,omitempty" yaml:"auth,omitempty"`
Expand All @@ -181,11 +225,11 @@ type HTTPConfiguration struct {
// tmpHTTPConfiguration is only used to custom the json/yaml marshalling/unmarshalling step.
// It shouldn't be used for other purpose.
type tmpHTTPConfiguration struct {
URL string `json:"url" yaml:"url"`
Access HTTPAccess `json:"access,omitempty" yaml:"access,omitempty"`
WhiteList []HTTPWhiteListConfiguration `json:"white_list" yaml:"white_list"`
Auth *HTTPAuth `json:"auth,omitempty" yaml:"auth,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
URL string `json:"url" yaml:"url"`
Access HTTPAccess `json:"access,omitempty" yaml:"access,omitempty"`
WhiteList []HTTPWhiteListConfig `json:"white_list" yaml:"white_list"`
Auth *HTTPAuth `json:"auth,omitempty" yaml:"auth,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
}

func (h *HTTPConfiguration) MarshalJSON() ([]byte, error) {
Expand Down