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

refactor(Providers) Move providers into their own tree. #67

Merged
merged 1 commit into from
Oct 15, 2017
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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: go
go:
- 1.8
- 1.9
# - tip

go_import_path: github.com/versent/unicreds
go_import_path: github.com/versent/saml2aws

install:
- echo noop
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ compile: deps
-osarch="windows/amd64" \
-osarch="windows/i386" \
-output "build/{{.Dir}}_$(VERSION)_{{.OS}}_{{.Arch}}/$(NAME)" \
$(shell glide novendor)
$(shell ./glide novendor)

install:
go install ./cmd/saml2aws
Expand All @@ -52,7 +52,7 @@ release:
@github-release "v$(VERSION)" dist/* --commit "$(git rev-parse HEAD)" --github-repository versent/$(NAME)

test: deps
go test -cover -v $(shell glide novendor)
go test -cover -v $(shell ./glide novendor)

clean:
rm ./glide
Expand Down
2 changes: 1 addition & 1 deletion cmd/saml2aws/commands/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/aws/aws-sdk-go/service/sts"
"github.com/pkg/errors"
"github.com/versent/saml2aws"
"github.com/versent/saml2aws/shell"
"github.com/versent/saml2aws/pkg/shell"
)

// Exec execute the supplied command after seeding the environment
Expand Down
5 changes: 3 additions & 2 deletions cmd/saml2aws/commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/pkg/errors"
"github.com/versent/saml2aws"
"github.com/versent/saml2aws/helper/credentials"
"github.com/versent/saml2aws/pkg/creds"
)

// LoginFlags login specific command flags
Expand Down Expand Up @@ -47,7 +48,7 @@ func Login(loginFlags *LoginFlags) error {

// fmt.Println("LookupCredentials", hostname)

loginDetails := &saml2aws.LoginDetails{
loginDetails := &creds.LoginDetails{
Hostname: hostname,
Username: username,
}
Expand Down Expand Up @@ -160,7 +161,7 @@ func Login(loginFlags *LoginFlags) error {
return nil
}

func resolveLoginDetails(loginDetails *saml2aws.LoginDetails, loginFlags *LoginFlags) error {
func resolveLoginDetails(loginDetails *creds.LoginDetails, loginFlags *LoginFlags) error {

// fmt.Printf("loginFlags %+v\n", loginFlags)

Expand Down
5 changes: 3 additions & 2 deletions cmd/saml2aws/commands/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import (

"github.com/stretchr/testify/assert"
"github.com/versent/saml2aws"
"github.com/versent/saml2aws/pkg/creds"
)

func TestResolveLoginDetailsWithFlags(t *testing.T) {

loginFlags := &LoginFlags{Hostname: "id.example.com", Username: "wolfeidau", Password: "testtestlol", SkipPrompt: true}

loginDetails := &saml2aws.LoginDetails{Hostname: "id.example.com", Username: ""}
loginDetails := &creds.LoginDetails{Hostname: "id.example.com", Username: ""}

err := resolveLoginDetails(loginDetails, loginFlags)

assert.Empty(t, err)
assert.Equal(t, loginDetails, &saml2aws.LoginDetails{Username: "wolfeidau", Password: "testtestlol", Hostname: "id.example.com"})
assert.Equal(t, loginDetails, &creds.LoginDetails{Username: "wolfeidau", Password: "testtestlol", Hostname: "id.example.com"})
}

func TestResolveRoleSingleEntry(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions helper/credentials/saml.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package credentials
import (
"fmt"

"github.com/versent/saml2aws"
"github.com/versent/saml2aws/pkg/creds"
)

// LookupCredentials lookup an existing set of credentials and validate it.
func LookupCredentials(loginDetails *saml2aws.LoginDetails) error {
func LookupCredentials(loginDetails *creds.LoginDetails) error {

username, password, err := CurrentHelper.Get(fmt.Sprintf("https://%s", loginDetails.Hostname))
if err != nil {
Expand Down
25 changes: 2 additions & 23 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,12 @@ import (
"strconv"
"strings"

"github.com/pkg/errors"
"github.com/segmentio/go-prompt"
"github.com/versent/saml2aws/pkg/creds"
)

// LoginDetails used to authenticate to ADFS
type LoginDetails struct {
Username string
Password string
Hostname string
}

// Validate validate the login details
func (ld *LoginDetails) Validate() error {
if ld.Hostname == "" {
return errors.New("Missing hostname")
}
if ld.Username == "" {
return errors.New("Missing username")
}
if ld.Password == "" {
return errors.New("Missing password")
}
return nil
}

// PromptForLoginDetails prompt the user to present their username, password and hostname
func PromptForLoginDetails(loginDetails *LoginDetails) error {
func PromptForLoginDetails(loginDetails *creds.LoginDetails) error {

loginDetails.Hostname = promptFor("Hostname [%s]", loginDetails.Hostname)

Expand Down
4 changes: 3 additions & 1 deletion input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package saml2aws

import (
"testing"

"github.com/versent/saml2aws/pkg/creds"
)

func TestLoginDetails_Validate(t *testing.T) {
Expand All @@ -23,7 +25,7 @@ func TestLoginDetails_Validate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ld := &LoginDetails{
ld := &creds.LoginDetails{
Username: tt.fields.Username,
Password: tt.fields.Password,
Hostname: tt.fields.Hostname,
Expand Down
24 changes: 24 additions & 0 deletions pkg/creds/creds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package creds

import "errors"

// LoginDetails used to authenticate
type LoginDetails struct {
Username string
Password string
Hostname string
}

// Validate validate the login details
func (ld *LoginDetails) Validate() error {
if ld.Hostname == "" {
return errors.New("Empty hostname")
}
if ld.Username == "" {
return errors.New("Empty username")
}
if ld.Password == "" {
return errors.New("Empty password")
}
return nil
}
52 changes: 52 additions & 0 deletions pkg/creds/creds_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package creds

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestValidateEmptyLoginDetails(t *testing.T) {

ld := &LoginDetails{}

err := ld.Validate()

require.Error(t, err)
}
func TestValidateEmptyHostnameLoginDetails(t *testing.T) {

ld := &LoginDetails{Username: "test", Password: "test"}

err := ld.Validate()

require.Error(t, err)

}

func TestValidateEmptyUsernameLoginDetails(t *testing.T) {

ld := &LoginDetails{Hostname: "test", Password: "test"}

err := ld.Validate()

require.Error(t, err)

}
func TestValidateEmptyPasswordLoginDetails(t *testing.T) {

ld := &LoginDetails{Hostname: "test", Username: "test"}

err := ld.Validate()

require.Error(t, err)
}

func TestValidateLoginDetails(t *testing.T) {

ld := &LoginDetails{Hostname: "test", Username: "test", Password: "test"}

err := ld.Validate()

require.Nil(t, err)
}
15 changes: 8 additions & 7 deletions adfs.go → pkg/provider/adfs/adfs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package saml2aws
package adfs

import (
"bytes"
Expand All @@ -13,17 +13,18 @@ import (

"github.com/PuerkitoBio/goquery"
"github.com/pkg/errors"
"github.com/versent/saml2aws/pkg/creds"

"golang.org/x/net/publicsuffix"
)

// ADFSClient wrapper around ADFS enabling authentication and retrieval of assertions
type ADFSClient struct {
// Client wrapper around ADFS enabling authentication and retrieval of assertions
type Client struct {
client *http.Client
}

// NewADFSClient create a new ADFS client
func NewADFSClient(skipVerify bool) (*ADFSClient, error) {
func NewADFSClient(skipVerify bool) (*Client, error) {

tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipVerify, Renegotiation: tls.RenegotiateFreelyAsClient},
Expand All @@ -40,13 +41,13 @@ func NewADFSClient(skipVerify bool) (*ADFSClient, error) {

client := &http.Client{Transport: tr, Jar: jar}

return &ADFSClient{
return &Client{
client: client,
}, nil
}

// Authenticate authenticate to ADFS and return the data from the body of the SAML assertion.
func (ac *ADFSClient) Authenticate(loginDetails *LoginDetails) (string, error) {
func (ac *Client) Authenticate(loginDetails *creds.LoginDetails) (string, error) {
var authSubmitURL string
var samlAssertion string
authForm := url.Values{}
Expand Down Expand Up @@ -122,7 +123,7 @@ func (ac *ADFSClient) Authenticate(loginDetails *LoginDetails) (string, error) {
return samlAssertion, nil
}

func updateFormData(authForm url.Values, s *goquery.Selection, user *LoginDetails) {
func updateFormData(authForm url.Values, s *goquery.Selection, user *creds.LoginDetails) {
name, ok := s.Attr("name")
// log.Printf("name = %s ok = %v", name, ok)
if !ok {
Expand Down
14 changes: 9 additions & 5 deletions adfs2.go → pkg/provider/adfs2/adfs2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package saml2aws
package adfs2

import (
"bytes"
Expand All @@ -14,14 +14,17 @@ import (
"github.com/Azure/go-ntlmssp"
"github.com/PuerkitoBio/goquery"
"github.com/pkg/errors"
"github.com/versent/saml2aws/pkg/creds"
)

type ADFS2Client struct {
// Client client for adfs2
type Client struct {
transport http.RoundTripper
jar http.CookieJar
}

func NewADFS2Client(skipVerify bool) (*ADFS2Client, error) {
// NewADFS2Client new adfs2 client with ntlmssp configured
func NewADFS2Client(skipVerify bool) (*Client, error) {
transport := &ntlmssp.Negotiator{
RoundTripper: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipVerify, Renegotiation: tls.RenegotiateFreelyAsClient},
Expand All @@ -35,13 +38,14 @@ func NewADFS2Client(skipVerify bool) (*ADFS2Client, error) {
return nil, err
}

return &ADFS2Client{
return &Client{
transport: transport,
jar: jar,
}, nil
}

func (ac *ADFS2Client) Authenticate(loginDetails *LoginDetails) (string, error) {
// Authenticate authenticate the user using the supplied login details
func (ac *Client) Authenticate(loginDetails *creds.LoginDetails) (string, error) {
var samlAssertion string
client := http.Client{
Transport: ac.transport,
Expand Down
15 changes: 8 additions & 7 deletions jumpcloud.go → pkg/provider/jumpcloud/jumpcloud.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package saml2aws
package jumpcloud

import (
"bytes"
Expand All @@ -14,17 +14,18 @@ import (
"github.com/PuerkitoBio/goquery"
"github.com/pkg/errors"
"github.com/segmentio/go-prompt"
"github.com/versent/saml2aws/pkg/creds"

"golang.org/x/net/publicsuffix"
)

// JumpCloudClient is a wrapper representing a JumpCloud SAML client
type JumpCloudClient struct {
// Client is a wrapper representing a JumpCloud SAML client
type Client struct {
client *http.Client
}

// NewJumpCloudClient creates a new JumpCloud client
func NewJumpCloudClient(skipVerify bool) (*JumpCloudClient, error) {
func NewJumpCloudClient(skipVerify bool) (*Client, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipVerify},
}
Expand All @@ -40,13 +41,13 @@ func NewJumpCloudClient(skipVerify bool) (*JumpCloudClient, error) {

client := &http.Client{Transport: tr, Jar: jar}

return &JumpCloudClient{
return &Client{
client: client,
}, nil
}

// Authenticate logs into JumpCloud and returns a SAML response
func (jc *JumpCloudClient) Authenticate(loginDetails *LoginDetails) (string, error) {
func (jc *Client) Authenticate(loginDetails *creds.LoginDetails) (string, error) {
var authSubmitURL string
var samlAssertion string
mfaRequired := false
Expand Down Expand Up @@ -159,7 +160,7 @@ func (jc *JumpCloudClient) Authenticate(loginDetails *LoginDetails) (string, err
return samlAssertion, nil
}

func updateJumpCloudForm(authForm url.Values, s *goquery.Selection, user *LoginDetails) {
func updateJumpCloudForm(authForm url.Values, s *goquery.Selection, user *creds.LoginDetails) {
name, ok := s.Attr("name")
if !ok {
return
Expand Down
Loading