Skip to content

Commit

Permalink
make golint happy
Browse files Browse the repository at this point in the history
  • Loading branch information
r3kzi committed Jun 9, 2020
1 parent 4587332 commit 2fd9b73
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ import (
"io/ioutil"
)

// Config is the root config for AWS Elasticsearch Service Provisioner
type Config struct {
Elasticsearch Elasticsearch `yaml:"elasticsearch"`
AWS AWS `yaml:"aws"`
}

// Elasticsearch contains all necessary configuration for an Elasticsearch Domain
type Elasticsearch struct {
Endpoint string `yaml:"endpoint"`
}

// AWS contains all AWS-specific configuration
type AWS struct {
Region string `yaml:"region"`
RoleARN string `yaml:"roleARN"`
}

// ParseConfig will parse a configuration file
func ParseConfig(filename string) (*Config, error) {
file, err := ioutil.ReadFile(filename)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"
)

// NewRequest will create a new HTTP request based on an URL and a Body string
func NewRequest(url string, body string) (*http.Request, error) {
req, err := http.NewRequest(http.MethodPut, url, strings.NewReader(body))
if err != nil {
Expand All @@ -18,6 +19,8 @@ func NewRequest(url string, body string) (*http.Request, error) {
return req, nil
}

// SignRequest will sign a HTTP requests with an assumed role for a specific AWS Region
// using AWS Signature V4 signing process
func SignRequest(req *http.Request, body string, creds *credentials.Credentials, service string, region string) (*http.Request, error) {
signer := v4.NewSigner(creds)
_, err := signer.Sign(req, strings.NewReader(body), service, region, time.Now())
Expand All @@ -27,6 +30,7 @@ func SignRequest(req *http.Request, body string, creds *credentials.Credentials,
return req, nil
}

// DoRequest will actually make the http call
func DoRequest(req *http.Request) (*http.Response, error) {
client := &http.Client{}
resp, err := client.Do(req)
Expand Down
14 changes: 12 additions & 2 deletions pkg/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (

const userAPIEndpoint = "_opendistro/_security/api/internalusers"

// User correspond a Kibana User
type User struct {
Password string `yaml:"password" json:"password"`
BackendRoles []string `yaml:"backend_roles" json:"backend_roles"`
}

// Read will a file und tries to parse User structs
func Read(filename string) (map[string]User, error) {
file, err := ioutil.ReadFile(filename)
if err != nil {
Expand All @@ -30,6 +32,7 @@ func Read(filename string) (map[string]User, error) {
return users, nil
}

// Create will create all User using the http package
func Create(users map[string]User, config *cfg.Config, creds *credentials.Credentials) error {
for key, value := range users {
jsonUser, err := json.Marshal(value)
Expand All @@ -50,15 +53,22 @@ func Create(users map[string]User, config *cfg.Config, creds *credentials.Creden
return fmt.Errorf(fmt.Sprintf("For user: %s, %s", key, err))
}

response, err := http.DoRequest(signRequest)
resp, err := http.DoRequest(signRequest)
if err != nil {
return fmt.Errorf(fmt.Sprintf("For user: %s, %s", key, err))
}
fmt.Println(response.StatusCode)

if resp.StatusCode != 200 && resp.StatusCode != 201 {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
bodyString := string(bodyBytes)
return fmt.Errorf(fmt.Sprintf("Failed creating user: %s, Status Code was %d\n"+
"Message: %s", key, resp.StatusCode, bodyString))
}
}
return nil
}

// Must ensures validate user map
func Must(users map[string]User, err error) map[string]User {
if err != nil {
panic(err)
Expand Down

0 comments on commit 2fd9b73

Please sign in to comment.