Skip to content

Commit

Permalink
Config updater user actions (#1)
Browse files Browse the repository at this point in the history
* Lingering `pivotalservices -> pivotal-michael-stergianis` name changes

* allow users of cf-mgmt to add users to orgs and spaces using a library
  like interface
  • Loading branch information
mstergianis authored Feb 24, 2020
1 parent 52d9640 commit 750625a
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 31 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ platform: linux

image_resource:
type: docker-image
source: {repository: pivotalservices/cf-mgmt, tag: "0.0.91"}
source: {repository: pivotal-michael-stergianis/cf-mgmt, tag: "0.0.91"}

inputs:
- name: config-repo
Expand Down Expand Up @@ -43,7 +43,7 @@ If there's already a configured foundation that you want to start using cf-mgmt

### Install

Compiled [releases](https://github.com/pivotalservices/cf-mgmt/releases) are available on Github.
Compiled [releases](https://github.com/pivotal-michael-stergianis/cf-mgmt/releases) are available on Github.
Download the binary for your platform and place it somewhere on your path.
Don't forget to `chmod +x` the file on Linux and macOS.

Expand Down Expand Up @@ -165,9 +165,9 @@ To build the binary yourself, follow these steps:
- Install `Go`.
- Install [Glide](https://github.com/Masterminds/glide), a dependency management tool for Go.
- Clone the repo:
- `mkdir -p $(go env GOPATH)/src/github.com/pivotalservices`
- `cd $(go env GOPATH)/src/github.com/pivotalservices`
- `git clone [email protected]:pivotalservices/cf-mgmt.git`
- `mkdir -p $(go env GOPATH)/src/github.com/pivotal-michael-stergianis`
- `cd $(go env GOPATH)/src/github.com/pivotal-michael-stergianis`
- `git clone [email protected]:pivotal-michael-stergianis/cf-mgmt.git`
- Install dependencies:
- `cd cf-mgmt`
- `glide install`
Expand Down
2 changes: 1 addition & 1 deletion ci/dev-build-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ resources:
- name: source
type: git
source:
uri: [email protected]:pivotalservices/cf-mgmt.git
uri: [email protected]:pivotal-michael-stergianis/cf-mgmt.git
branch: {{dev-build-branch}}
private_key: {{git-ssh-key}}
ignore_paths: ["README.md","docs/*"]
Expand Down
6 changes: 3 additions & 3 deletions ci/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ resources:
- name: source
type: git
source:
uri: [email protected]:pivotalservices/cf-mgmt.git
uri: [email protected]:pivotal-michael-stergianis/cf-mgmt.git
branch: master
private_key: {{git-ssh-key}}
ignore_paths: ["README.md","docs/*"]

- name: draft-cfmgmt-resource
type: github-release
source:
owner: pivotalservices
owner: pivotal-michael-stergianis
repository: cf-mgmt
access_token: {{github-token}}
drafts: true

- name: releases
type: github-release
source:
owner: pivotalservices
owner: pivotal-michael-stergianis
repository: cf-mgmt
access_token: {{github-token}}

Expand Down
14 changes: 7 additions & 7 deletions ci/tasks/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ else
fi
echo "next version should be: ${DRAFT_VERSION}"

WORKING_DIR=$GOPATH/src/github.com/pivotalservices/cf-mgmt
WORKING_DIR=$GOPATH/src/github.com/pivotal-michael-stergianis/cf-mgmt
mkdir -p ${WORKING_DIR}
cp -R ${SOURCE_DIR}/* ${WORKING_DIR}/.
cd ${WORKING_DIR}
glide install
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${OUTPUT_DIR}/cf-mgmt-linux -ldflags "-X github.com/pivotalservices/cf-mgmt/configcommands.VERSION=${VERSION} -X github.com/pivotalservices/cf-mgmt/configcommands.COMMIT=${COMMIT}" cmd/cf-mgmt/main.go
GOOS=darwin GOARCH=amd64 go build -o ${OUTPUT_DIR}/cf-mgmt-osx -ldflags "-X github.com/pivotalservices/cf-mgmt/configcommands.VERSION=${VERSION} -X github.com/pivotalservices/cf-mgmt/configcommands.COMMIT=${COMMIT}" cmd/cf-mgmt/main.go
GOOS=windows GOARCH=amd64 go build -o ${OUTPUT_DIR}/cf-mgmt.exe -ldflags "-X github.com/pivotalservices/cf-mgmt/configcommands.VERSION=${VERSION} -X github.com/pivotalservices/cf-mgmt/configcommands.COMMIT=${COMMIT}" cmd/cf-mgmt/main.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${OUTPUT_DIR}/cf-mgmt-linux -ldflags "-X github.com/pivotal-michael-stergianis/cf-mgmt/configcommands.VERSION=${VERSION} -X github.com/pivotal-michael-stergianis/cf-mgmt/configcommands.COMMIT=${COMMIT}" cmd/cf-mgmt/main.go
GOOS=darwin GOARCH=amd64 go build -o ${OUTPUT_DIR}/cf-mgmt-osx -ldflags "-X github.com/pivotal-michael-stergianis/cf-mgmt/configcommands.VERSION=${VERSION} -X github.com/pivotal-michael-stergianis/cf-mgmt/configcommands.COMMIT=${COMMIT}" cmd/cf-mgmt/main.go
GOOS=windows GOARCH=amd64 go build -o ${OUTPUT_DIR}/cf-mgmt.exe -ldflags "-X github.com/pivotal-michael-stergianis/cf-mgmt/configcommands.VERSION=${VERSION} -X github.com/pivotal-michael-stergianis/cf-mgmt/configcommands.COMMIT=${COMMIT}" cmd/cf-mgmt/main.go

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${OUTPUT_DIR}/cf-mgmt-config-linux -ldflags "-X github.com/pivotalservices/cf-mgmt/configcommands.VERSION=${VERSION} -X github.com/pivotalservices/cf-mgmt/configcommands.COMMIT=${COMMIT}" cmd/cf-mgmt-config/main.go
GOOS=darwin GOARCH=amd64 go build -o ${OUTPUT_DIR}/cf-mgmt-config-osx -ldflags "-X github.com/pivotalservices/cf-mgmt/configcommands.VERSION=${VERSION} -X github.com/pivotalservices/cf-mgmt/configcommands.COMMIT=${COMMIT}" cmd/cf-mgmt-config/main.go
GOOS=windows GOARCH=amd64 go build -o ${OUTPUT_DIR}/cf-mgmt-config.exe -ldflags "-X github.com/pivotalservices/cf-mgmt/configcommands.VERSION=${VERSION} -X github.com/pivotalservices/cf-mgmt/configcommands.COMMIT=${COMMIT}" cmd/cf-mgmt-config/main.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${OUTPUT_DIR}/cf-mgmt-config-linux -ldflags "-X github.com/pivotal-michael-stergianis/cf-mgmt/configcommands.VERSION=${VERSION} -X github.com/pivotal-michael-stergianis/cf-mgmt/configcommands.COMMIT=${COMMIT}" cmd/cf-mgmt-config/main.go
GOOS=darwin GOARCH=amd64 go build -o ${OUTPUT_DIR}/cf-mgmt-config-osx -ldflags "-X github.com/pivotal-michael-stergianis/cf-mgmt/configcommands.VERSION=${VERSION} -X github.com/pivotal-michael-stergianis/cf-mgmt/configcommands.COMMIT=${COMMIT}" cmd/cf-mgmt-config/main.go
GOOS=windows GOARCH=amd64 go build -o ${OUTPUT_DIR}/cf-mgmt-config.exe -ldflags "-X github.com/pivotal-michael-stergianis/cf-mgmt/configcommands.VERSION=${VERSION} -X github.com/pivotal-michael-stergianis/cf-mgmt/configcommands.COMMIT=${COMMIT}" cmd/cf-mgmt-config/main.go

echo ${DRAFT_VERSION} > ${OUTPUT_DIR}/name
echo ${DRAFT_VERSION} > ${OUTPUT_DIR}/tag
2 changes: 1 addition & 1 deletion ci/tasks/runTests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export PATH=$GOPATH/bin:$PATH
mkdir -p $GOPATH/bin
curl https://glide.sh/get | sh

WORKING_DIR=$GOPATH/src/github.com/pivotalservices/cf-mgmt
WORKING_DIR=$GOPATH/src/github.com/pivotal-michael-stergianis/cf-mgmt
mkdir -p ${WORKING_DIR}
cp -R source/* ${WORKING_DIR}/.
cd ${WORKING_DIR}
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type Updater interface {
CreateConfigIfNotExists(uaaOrigin string) error
DeleteConfigIfExists() error

AssociateOrgAuditor(orgName, user string) error
AssociateSpaceDeveloper(orgName, spaceName, user string) error
AssociateSpaceAuditor(orgName, spaceName, user string) error

SaveOrgSpaces(spaces *Spaces) error
SaveSpaceConfig(spaceConfig *SpaceConfig) error
SaveOrgConfig(orgConfig *OrgConfig) error
Expand Down
14 changes: 14 additions & 0 deletions config/usermgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@ func (u *UserMgmt) groups(groupName string) []string {
}
return result
}

func (u *UserMgmt) hasUAAUser(user string) bool {
for _, existentUser := range u.Users {
if user == existentUser {
return true
}
}

return false
}

func (u *UserMgmt) addUAAUser(user string) {
u.Users = append(u.Users, user)
}
65 changes: 65 additions & 0 deletions config/yaml_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,68 @@ func (m *yamlManager) SaveSpaceQuota(spaceQuota *SpaceQuota) error {
fmt.Println(fmt.Sprintf("Saving Named Space Quote %s for org %s", spaceQuota.Name, spaceQuota.Org))
return WriteFile(targetFile, spaceQuota)
}

type userRole int

const (
auditorRole userRole = iota
developerRole
)

func (m *yamlManager) AssociateOrgAuditor(orgName, user string) error {
orgConfig, err := m.GetOrgConfig(orgName)
if err != nil {
return err
}

if orgConfig.Auditor.hasUAAUser(user) {
return nil
}

// TODO: generalize so we can add users to correct origin
orgConfig.Auditor.addUAAUser(user)
if err = m.SaveOrgConfig(orgConfig); err != nil {
return err
}

return nil
}

func (m *yamlManager) AssociateSpaceAuditor(orgName, spaceName, user string) error {
return m.associateSpaceRole(auditorRole, orgName, spaceName, user)
}

func (m *yamlManager) AssociateSpaceDeveloper(orgName, spaceName, user string) error {
return m.associateSpaceRole(developerRole, orgName, spaceName, user)
}

func (m *yamlManager) associateSpaceRole(role userRole, orgName, spaceName, user string) error {
spaceConfig, err := m.GetSpaceConfig(orgName, spaceName)
if err != nil {
return err
}

if spaceConfig.Developer.hasUAAUser(user) {
return nil
}

var userManager *UserMgmt
switch role {
case auditorRole:
userManager = &spaceConfig.Auditor
case developerRole:
userManager = &spaceConfig.Developer

// this cannot be tested as we cannot call unexported methods from the test package
default:
return errors.New("an invalid space role was provided")
}

// TODO: generalize so we can add users to correct origin
userManager.addUAAUser(user)
if err = m.SaveSpaceConfig(spaceConfig); err != nil {
return err
}

return nil
}
176 changes: 176 additions & 0 deletions config/yaml_config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config_test

import (
"fmt"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -476,4 +477,179 @@ var _ = Describe("CF-Mgmt Config", func() {

})
})

Context("YAML Config Updater", func() {
Context("User Actions", func() {
var tempDir string
var configManager config.Manager

BeforeEach(func() {
var err error
tempDir, err = ioutil.TempDir("", "cf-mgmt")
Expect(err).ShouldNot(HaveOccurred())
configManager = config.NewManager(path.Join(tempDir, "cfmgmt"))
configManager.CreateConfigIfNotExists("ldap")
})

AfterEach(func() {
os.RemoveAll(tempDir)
})

Context("Associate Org Auditor", func() {
When("the org exists", func() {
const (
orgName = "the-org"
userName = "the-user"
)
BeforeEach(func() {
err := configManager.AddOrgToConfig(&config.OrgConfig{
Org: orgName,
})
Expect(err).ShouldNot(HaveOccurred())
})

When("the user does not exist", func() {
It("adds the user to the org", func() {
o, err := configManager.GetOrgConfig(orgName)
Expect(err).ShouldNot(HaveOccurred())
Expect(o.Auditor.Users).Should(HaveLen(0))

err = configManager.AssociateOrgAuditor(orgName, userName)
Expect(err).ShouldNot(HaveOccurred())

o, err = configManager.GetOrgConfig(orgName)
Expect(err).ShouldNot(HaveOccurred())
Expect(o.Auditor.Users).Should(HaveLen(1))
})
})

When("the user already exists", func() {
BeforeEach(func() {
o, err := configManager.GetOrgConfig(orgName)
Expect(err).ShouldNot(HaveOccurred())

o.Auditor.Users = append(o.Auditor.Users, userName)

err = configManager.SaveOrgConfig(o)
Expect(err).ShouldNot(HaveOccurred())
})

It("does nothing and returns nil", func() {
o, err := configManager.GetOrgConfig(orgName)
Expect(err).ShouldNot(HaveOccurred())
Expect(o.Auditor.Users).Should(HaveLen(1))

err = configManager.AssociateOrgAuditor(orgName, userName)
Expect(err).ShouldNot(HaveOccurred())

o, err = configManager.GetOrgConfig(orgName)
Expect(err).ShouldNot(HaveOccurred())
Expect(o.Auditor.Users).Should(HaveLen(1))
})
})
})

When("the org does not exist", func() {
It("returns an error", func() {
orgName := "org-that-does-not-exist"
err := configManager.AssociateOrgAuditor(orgName, "my-user")
Expect(err).Should(HaveOccurred())
Expect(err).Should(MatchError(fmt.Sprintf("Org [%s] not found in config", orgName)))
})
})
})

Context("Associate Space Role", func() {
When("the org and space exist", func() {
const (
orgName = "the-org"
spaceName = "the-space"
userName = "the-user"
)
BeforeEach(func() {
err := configManager.AddOrgToConfig(&config.OrgConfig{
Org: orgName,
})
Expect(err).ShouldNot(HaveOccurred())

err = configManager.SaveOrgSpaces(&config.Spaces{
Org: orgName,
})
Expect(err).ShouldNot(HaveOccurred())

err = configManager.AddSpaceToConfig(&config.SpaceConfig{
Org: orgName,
Space: spaceName,
})
Expect(err).ShouldNot(HaveOccurred())
})

When("the user does not exist in the space", func() {
It("creates the user with a developer role", func() {
s, err := configManager.GetSpaceConfig(orgName, spaceName)
Expect(err).ShouldNot(HaveOccurred())
Expect(s.Developer.Users).Should(HaveLen(0))

err = configManager.AssociateSpaceDeveloper(orgName, spaceName, userName)
Expect(err).ShouldNot(HaveOccurred())

s, err = configManager.GetSpaceConfig(orgName, spaceName)
Expect(err).ShouldNot(HaveOccurred())
Expect(s.Developer.Users).Should(HaveLen(1))
})

It("creates the user with an auditor role", func() {
s, err := configManager.GetSpaceConfig(orgName, spaceName)
Expect(err).ShouldNot(HaveOccurred())
Expect(s.Developer.Users).Should(HaveLen(0))

err = configManager.AssociateSpaceAuditor(orgName, spaceName, userName)
Expect(err).ShouldNot(HaveOccurred())

s, err = configManager.GetSpaceConfig(orgName, spaceName)
Expect(err).ShouldNot(HaveOccurred())
Expect(s.Auditor.Users).Should(HaveLen(1))
})
})

When("user already exists in space", func() {
BeforeEach(func() {
s, err := configManager.GetSpaceConfig(orgName, spaceName)
Expect(err).ShouldNot(HaveOccurred())

s.Developer.Users = append(s.Developer.Users, userName)

err = configManager.SaveSpaceConfig(s)
Expect(err).ShouldNot(HaveOccurred())
})

It("does nothing and returns nil", func() {
s, err := configManager.GetSpaceConfig(orgName, spaceName)
Expect(err).ShouldNot(HaveOccurred())
Expect(s.Developer.Users).Should(HaveLen(1))

err = configManager.AssociateSpaceDeveloper(orgName, spaceName, userName)
Expect(err).ShouldNot(HaveOccurred())

s, err = configManager.GetSpaceConfig(orgName, spaceName)
Expect(err).ShouldNot(HaveOccurred())
Expect(s.Developer.Users).Should(HaveLen(1))
})
})
})

When("the space cannot be found", func() {
It("returns an error", func() {
const (
orgName = "org-that-maybe-exists"
spaceName = "space-that-does-not-exist"
)
err := configManager.AssociateSpaceDeveloper(orgName, spaceName, "my-user")
Expect(err).Should(HaveOccurred())
Expect(err).Should(MatchError(fmt.Sprintf("Space [%s] not found in org [%s] config", spaceName, orgName)))
})
})
})
})
})
})
Loading

0 comments on commit 750625a

Please sign in to comment.