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

Add Email-Id and Access-Token Headers for authentication #15

Merged
merged 1 commit into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go:

env:
global:
- ENVIRONMENT="test"
- PROCTOR_KUBE_CONFIG="out-of-cluster"
- PROCTOR_LOG_LEVEL="debug"
- PROCTOR_APP_PORT="5000"
Expand Down Expand Up @@ -54,4 +55,4 @@ jobs:
# testing proctor cli
- cd ../.
- glide install
- go test -race -cover ./cmd/... ./config/... ./io/... ./proc/... ./engine/... .
- go test -race -cover ./cmd/... ./config/... ./io/... ./proc/... ./daemon/... . -v
9 changes: 8 additions & 1 deletion cmd/procs/description/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package description

import (
"fmt"
"net/http"

"github.com/fatih/color"
"github.com/gojektech/proctor/daemon"
"github.com/gojektech/proctor/io"
"github.com/gojektech/proctor/proc"
"github.com/gojektech/proctor/proctord/utility"
"github.com/spf13/cobra"
)

Expand All @@ -18,7 +20,12 @@ func NewCmd(printer io.Printer, proctorEngineClient daemon.Client) *cobra.Comman
Run: func(cmd *cobra.Command, args []string) {
procList, err := proctorEngineClient.ListProcs()
if err != nil {
printer.Println("Error fetching list of procs. Please check configuration and network connectivity", color.FgRed)
if err.Error() == http.StatusText(http.StatusUnauthorized) {
printer.Println(utility.UnauthorizedError, color.FgRed)
return
}

printer.Println(utility.GenericDescribeCmdError, color.FgRed)
return
}

Expand Down
14 changes: 13 additions & 1 deletion cmd/procs/description/descriptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package description
import (
"errors"
"fmt"
"net/http"
"testing"

"github.com/fatih/color"
"github.com/gojektech/proctor/daemon"
"github.com/gojektech/proctor/io"
"github.com/gojektech/proctor/proc"
"github.com/gojektech/proctor/proc/env"
"github.com/gojektech/proctor/proctord/utility"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -76,7 +78,7 @@ func (s *DescribeCmdTestSuite) TestDescribeCmdRun() {

func (s *DescribeCmdTestSuite) TestDescribeCmdRunProctorEngineClientFailure() {
s.mockProctorEngineClient.On("ListProcs").Return([]proc.Metadata{}, errors.New("error")).Once()
s.mockPrinter.On("Println", "Error fetching list of procs. Please check configuration and network connectivity", color.FgRed).Once()
s.mockPrinter.On("Println", utility.GenericDescribeCmdError, color.FgRed).Once()

s.testDescribeCmd.Run(&cobra.Command{}, []string{})

Expand All @@ -94,6 +96,16 @@ func (s *DescribeCmdTestSuite) TestDescribeCmdRunProcNotSupported() {
s.mockPrinter.AssertExpectations(s.T())
}

func (s *DescribeCmdTestSuite) TestDescribeCmdRunProcForUnauthorizedUser() {
s.mockProctorEngineClient.On("ListProcs").Return([]proc.Metadata{}, errors.New(http.StatusText(http.StatusUnauthorized))).Once()
s.mockPrinter.On("Println", utility.UnauthorizedError, color.FgRed).Once()

s.testDescribeCmd.Run(&cobra.Command{}, []string{"any-proc"})

s.mockProctorEngineClient.AssertExpectations(s.T())
s.mockPrinter.AssertExpectations(s.T())
}

func TestDescribeCmdTestSuite(t *testing.T) {
suite.Run(t, new(DescribeCmdTestSuite))
}
12 changes: 9 additions & 3 deletions cmd/procs/execution/executioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package execution

import (
"fmt"
"net/http"
"strings"

"github.com/fatih/color"
"github.com/gojektech/proctor/daemon"
"github.com/gojektech/proctor/io"
"github.com/gojektech/proctor/proctord/utility"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -47,18 +49,22 @@ func NewCmd(printer io.Printer, proctorEngineClient daemon.Client) *cobra.Comman

executedProcName, err := proctorEngineClient.ExecuteProc(procName, procArgs)
if err != nil {
printer.Println("\nError executing proc. Please check configuration and network connectivity", color.FgRed)
if err.Error() == http.StatusText(http.StatusUnauthorized) {
printer.Println(utility.UnauthorizedError, color.FgRed)
return
}
printer.Println(utility.GenericProcCmdError, color.FgRed)
return
}

printer.Println("Proc execution successful. \nStreaming logs:", color.FgGreen)
err = proctorEngineClient.StreamProcLogs(executedProcName)
if err != nil {
printer.Println("\nError Streaming Logs", color.FgRed)
printer.Println("Error Streaming Logs", color.FgRed)
return
}

printer.Println("\nLog stream of proc completed.", color.FgGreen)
printer.Println("Log stream of proc completed.", color.FgGreen)
},
}
}
29 changes: 24 additions & 5 deletions cmd/procs/execution/executioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package execution
import (
"errors"
"fmt"
"net/http"
"testing"

"github.com/fatih/color"
"github.com/gojektech/proctor/daemon"
"github.com/gojektech/proctor/io"
"github.com/gojektech/proctor/proctord/utility"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -51,7 +53,7 @@ func (s *ExecutionCmdTestSuite) TestExecutionCmd() {
s.mockPrinter.On("Println", "Proc execution successful. \nStreaming logs:", color.FgGreen).Once()

s.mockProctorEngineClient.On("StreamProcLogs", "executed-proc-name").Return(nil).Once()
s.mockPrinter.On("Println", "\nLog stream of proc completed.", color.FgGreen).Once()
s.mockPrinter.On("Println", "Log stream of proc completed.", color.FgGreen).Once()

s.testExecutionCmd.Run(&cobra.Command{}, args)

Expand Down Expand Up @@ -79,7 +81,7 @@ func (s *ExecutionCmdTestSuite) TestExecutionCmdForNoProcVariables() {
s.mockPrinter.On("Println", "Proc execution successful. \nStreaming logs:", color.FgGreen).Once()

s.mockProctorEngineClient.On("StreamProcLogs", "executed-proc-name").Return(nil).Once()
s.mockPrinter.On("Println", "\nLog stream of proc completed.", color.FgGreen).Once()
s.mockPrinter.On("Println", "Log stream of proc completed.", color.FgGreen).Once()

s.testExecutionCmd.Run(&cobra.Command{}, args)

Expand All @@ -100,7 +102,7 @@ func (s *ExecutionCmdTestSuite) TestExecutionCmdForIncorrectVariableFormat() {
s.mockPrinter.On("Println", "Proc execution successful. \nStreaming logs:", color.FgGreen).Once()

s.mockProctorEngineClient.On("StreamProcLogs", "executed-proc-name").Return(nil).Once()
s.mockPrinter.On("Println", "\nLog stream of proc completed.", color.FgGreen).Once()
s.mockPrinter.On("Println", "Log stream of proc completed.", color.FgGreen).Once()

s.testExecutionCmd.Run(&cobra.Command{}, args)

Expand All @@ -117,7 +119,24 @@ func (s *ExecutionCmdTestSuite) TestExecutionCmdForProctorEngineExecutionFailure
procArgs := make(map[string]string)
s.mockProctorEngineClient.On("ExecuteProc", "say-hello-world", procArgs).Return("", errors.New("error")).Once()

s.mockPrinter.On("Println", "\nError executing proc. Please check configuration and network connectivity", color.FgRed).Once()
s.mockPrinter.On("Println", utility.GenericProcCmdError, color.FgRed).Once()

s.testExecutionCmd.Run(&cobra.Command{}, args)

s.mockProctorEngineClient.AssertExpectations(s.T())
s.mockPrinter.AssertExpectations(s.T())
}

func (s *ExecutionCmdTestSuite) TestExecutionCmdForProctorEngineExecutionForUnauthorizedUser() {
args := []string{"say-hello-world"}

s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", "Executing Proc", "say-hello-world"), color.Reset).Once()
s.mockPrinter.On("Println", "With No Variables", color.FgRed).Once()

procArgs := make(map[string]string)
s.mockProctorEngineClient.On("ExecuteProc", "say-hello-world", procArgs).Return("", errors.New(http.StatusText(http.StatusUnauthorized))).Once()

s.mockPrinter.On("Println", utility.UnauthorizedError, color.FgRed).Once()

s.testExecutionCmd.Run(&cobra.Command{}, args)

Expand All @@ -137,7 +156,7 @@ func (s *ExecutionCmdTestSuite) TestExecutionCmdForProctorEngineLogStreamingFail
s.mockPrinter.On("Println", "Proc execution successful. \nStreaming logs:", color.FgGreen).Once()

s.mockProctorEngineClient.On("StreamProcLogs", "executed-proc-name").Return(errors.New("error")).Once()
s.mockPrinter.On("Println", "\nError Streaming Logs", color.FgRed).Once()
s.mockPrinter.On("Println", "Error Streaming Logs", color.FgRed).Once()

s.testExecutionCmd.Run(&cobra.Command{}, args)

Expand Down
9 changes: 8 additions & 1 deletion cmd/procs/list/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package list

import (
"fmt"
"net/http"

"github.com/fatih/color"
"github.com/gojektech/proctor/daemon"
"github.com/gojektech/proctor/io"
"github.com/gojektech/proctor/proctord/utility"
"github.com/spf13/cobra"
)

Expand All @@ -17,7 +19,12 @@ func NewCmd(printer io.Printer, proctorEngineClient daemon.Client) *cobra.Comman
Run: func(cmd *cobra.Command, args []string) {
procList, err := proctorEngineClient.ListProcs()
if err != nil {
printer.Println("Error fetching list of procs. Please check configuration and network connectivity", color.FgRed)
if err.Error() == http.StatusText(http.StatusUnauthorized) {
printer.Println(utility.UnauthorizedError, color.FgRed)
return
}

printer.Println(utility.GenericListCmdError, color.FgRed)
return
}

Expand Down
14 changes: 13 additions & 1 deletion cmd/procs/list/lister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package list
import (
"errors"
"fmt"
"net/http"
"testing"

"github.com/fatih/color"
"github.com/gojektech/proctor/daemon"
"github.com/gojektech/proctor/io"
"github.com/gojektech/proctor/proc"
"github.com/gojektech/proctor/proctord/utility"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -62,7 +64,17 @@ func (s *ListCmdTestSuite) TestListCmdRun() {

func (s *ListCmdTestSuite) TestListCmdRunProctorEngineClientFailure() {
s.mockProctorEngineClient.On("ListProcs").Return([]proc.Metadata{}, errors.New("error")).Once()
s.mockPrinter.On("Println", "Error fetching list of procs. Please check configuration and network connectivity", color.FgRed).Once()
s.mockPrinter.On("Println", utility.GenericListCmdError, color.FgRed).Once()

s.testListCmd.Run(&cobra.Command{}, []string{})

s.mockProctorEngineClient.AssertExpectations(s.T())
s.mockPrinter.AssertExpectations(s.T())
}

func (s *ListCmdTestSuite) TestListCmdRunProctorEngineClientForUnauthorizedUser() {
s.mockProctorEngineClient.On("ListProcs").Return([]proc.Metadata{}, errors.New(http.StatusText(http.StatusUnauthorized))).Once()
s.mockPrinter.On("Println", utility.UnauthorizedError, color.FgRed).Once()

s.testListCmd.Run(&cobra.Command{}, []string{})

Expand Down
29 changes: 21 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ import (

func InitConfig() {
viper.SetConfigType("yaml")
viper.AutomaticEnv()
var configFileDir string

home := "$HOME/.proctor"
viper.AddConfigPath(home)
if viper.GetString("ENVIRONMENT") == "test" {
configFileDir = "/tmp"
} else {
configFileDir = "$HOME/.proctor"
}

viper.AddConfigPath(configFileDir)
viper.SetConfigName("proctor")

viper.AutomaticEnv()

err := viper.ReadInConfig()

if err != nil {
fmt.Println("Error reading proctor config")
os.Exit(1)
Expand All @@ -27,9 +32,17 @@ func InitConfig() {
func ProctorURL() string {
InitConfig()
proctorUrl := viper.GetString("PROCTOR_URL")
if len(proctorUrl) == 0 {
fmt.Println("proctor url not configured")
os.Exit(1)
}
return proctorUrl
}

func EmailId() string {
InitConfig()
emailId := viper.GetString("EMAIL_ID")
return emailId
}

func AccessToken() string {
InitConfig()
accessToken := viper.GetString("ACCESS_TOKEN")
return accessToken
}
49 changes: 28 additions & 21 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,41 @@ import (
)

func TestProctorURL(t *testing.T) {
proctorConfigFileExistedBeforeTest := true

home := os.Getenv("HOME")
proctorConfigDir := home + "/.proctor"
proctorConfigFilePath := proctorConfigDir + "/proctor.yaml"
existingConfigFileData, err := ioutil.ReadFile(proctorConfigFilePath)
if err != nil {
proctorConfigFileExistedBeforeTest = false
os.Mkdir(proctorConfigDir, os.ModePerm)
}

proctorConfigFilePath := "/tmp/proctor.yaml"
proctorUrl := []byte("PROCTOR_URL: any-random-url.com")
err = ioutil.WriteFile(proctorConfigFilePath, proctorUrl, 0644)
err := ioutil.WriteFile(proctorConfigFilePath, proctorUrl, 0644)
defer os.Remove(proctorConfigFilePath)
assert.NoError(t, err)

config.InitConfig()
configuredProctorURL := config.ProctorURL()

assert.Equal(t, "any-random-url.com", configuredProctorURL)
}

func TestProctorEmailId(t *testing.T) {
proctorConfigFilePath := "/tmp/proctor.yaml"
EmailId := []byte("EMAIL_ID: foobar@gmail.com")
err := ioutil.WriteFile(proctorConfigFilePath, EmailId, 0644)
defer os.Remove(proctorConfigFilePath)
assert.NoError(t, err)

config.InitConfig()
configuredEmailId := config.EmailId()

if proctorConfigFileExistedBeforeTest {
err = ioutil.WriteFile(proctorConfigFilePath, existingConfigFileData, 0644)
assert.NoError(t, err)
} else {
err = os.Remove(proctorConfigFilePath)
assert.NoError(t, err)
assert.Equal(t, "foobar@gmail.com", configuredEmailId)
}

func TestProctorAccessToken(t *testing.T) {
proctorConfigFilePath := "/tmp/proctor.yaml"

AccessToken := []byte("ACCESS_TOKEN: access-token")
err := ioutil.WriteFile(proctorConfigFilePath, AccessToken, 0644)
defer os.Remove(proctorConfigFilePath)
assert.NoError(t, err)

config.InitConfig()
configuredAccessToken := config.AccessToken()

err = os.Remove(proctorConfigDir)
assert.NoError(t, err)
}
assert.Equal(t, "access-token", configuredAccessToken)
}
Loading