Skip to content

Commit

Permalink
Add pks-login command to Hammer (#6)
Browse files Browse the repository at this point in the history
* Add pks-login command to Hammer

* Requires the API for PKS (configurable in OpsManager) to be in Env
file

* Remove parseUrl and inline parsing of urls and ip

* Run go mod tidy
  • Loading branch information
gmrodgers authored Aug 29, 2019
1 parent dba4785 commit cd939cc
Show file tree
Hide file tree
Showing 19 changed files with 477 additions and 12 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ In order to run the `hammer` tool against a given environment you need to have a
},
"ops_manager_private_key": "OPSMAN-RSA-PRIVATE-KEY",
"ops_manager_public_ip": "OPSMAN-PUBLIC-IP",
"sys_domain": "PAS-SYSTEM-DOMAIN"
"sys_domain": "PAS-SYSTEM-DOMAIN",
"pks_api": {
"url": "PKS-API-FQDN"
}
}
```
This file can then be passed into the tool via `hammer -t path-to-env-config.json <command>`.
Expand Down
2 changes: 1 addition & 1 deletion commands/cf_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c *CFLoginCommand) Execute(args []string) error {
return err
}

c.UI.DisplayText(fmt.Sprintf("Logging in to: %s\n", data.OpsManager.URL.String()))
c.UI.DisplayText(fmt.Sprintf("Logging in to CF at: %s\n", data.OpsManager.URL.String()))

return c.CFLoginRunner.Run(data, c.File)
}
2 changes: 1 addition & 1 deletion commands/cf_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var _ = Describe("cf login command", func() {

It("displays that the cf is being logged into", func() {
Expect(ui.DisplayTextCallCount()).To(Equal(1))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("Logging in to: www.test-cf.io\n"))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("Logging in to CF at: www.test-cf.io\n"))
})

It("runs the cf login tool using the retrieved environment config", func() {
Expand Down
35 changes: 35 additions & 0 deletions commands/pks_login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright (C) 2019-Present Pivotal Software, Inc. All rights reserved.
This program and the accompanying materials are made available under the terms of the under the Apache License, Version 2.0 (the "License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

package commands

import (
"fmt"
)

type PKSLoginCommand struct {
TargetConfig string `short:"t" long:"target" env:"TARGET_ENVIRONMENT_CONFIG" hidden:"true"`
File bool `short:"f" long:"file" description:"write a script file but do not run it"`

Env EnvReader
UI UI
PKSLoginRunner ToolRunner
}

func (c *PKSLoginCommand) Execute(args []string) error {
data, err := c.Env.Read(c.TargetConfig)
if err != nil {
return err
}

c.UI.DisplayText(fmt.Sprintf("Logging in to PKS at: %s\n", data.OpsManager.URL.String()))

return c.PKSLoginRunner.Run(data, c.File)
}
141 changes: 141 additions & 0 deletions commands/pks_login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
Copyright (C) 2019-Present Pivotal Software, Inc. All rights reserved.
This program and the accompanying materials are made available under the terms of the under the Apache License, Version 2.0 (the "License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

package commands_test

import (
"fmt"
"net/url"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
fakes "github.com/pivotal/hammer/commands/commandsfakes"
"github.com/pivotal/hammer/environment"

. "github.com/pivotal/hammer/commands"
)

var _ = Describe("pks login command", func() {
var (
err error
command *PKSLoginCommand

envReader *fakes.FakeEnvReader
ui *fakes.FakeUI
pksLoginRunner *fakes.FakeToolRunner
commandArgs []string
)

BeforeEach(func() {
envReader = new(fakes.FakeEnvReader)
ui = new(fakes.FakeUI)
pksLoginRunner = new(fakes.FakeToolRunner)
commandArgs = []string{"arg1", "arg2"}

command = &PKSLoginCommand{
Env: envReader,
UI: ui,
PKSLoginRunner: pksLoginRunner,
File: true,
}
})

JustBeforeEach(func() {
err = command.Execute(commandArgs)
})

When("retrieving the environment config errors", func() {
BeforeEach(func() {
envReader.ReadReturns(environment.Config{}, fmt.Errorf("env-reader-error"))
})

It("doesn't attempt to run the pks login tool", func() {
Expect(pksLoginRunner.RunCallCount()).To(Equal(0))
})

It("propagates the error", func() {
Expect(err).To(MatchError("env-reader-error"))
})
})

When("retrieving the environment config is successful", func() {
BeforeEach(func() {
url, _ := url.Parse("www.test-pks.io")
envReader.ReadReturns(environment.Config{OpsManager: environment.OpsManager{URL: *url}}, nil)
})

It("displays that the pks is being logged into", func() {
Expect(ui.DisplayTextCallCount()).To(Equal(1))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("Logging in to PKS at: www.test-pks.io\n"))
})

It("runs the pks login tool using the retrieved environment config", func() {
Expect(pksLoginRunner.RunCallCount()).To(Equal(1))

environmentConfig, _, _ := pksLoginRunner.RunArgsForCall(0)

expectedURL, _ := url.Parse("www.test-pks.io")
Expect(environmentConfig).To(BeEquivalentTo(environment.Config{OpsManager: environment.OpsManager{URL: *expectedURL}}))
})

When("run with the file flag set", func() {
BeforeEach(func() {
command.File = true
})

It("runs the pks login tool in dry run mode", func() {
Expect(pksLoginRunner.RunCallCount()).To(Equal(1))

_, dryRun, _ := pksLoginRunner.RunArgsForCall(0)
Expect(dryRun).To(BeTrue())
})
})

When("run with the file flag unset", func() {
BeforeEach(func() {
command.File = false
})

It("runs the pks login tool in non-dry run mode", func() {
Expect(pksLoginRunner.RunCallCount()).To(Equal(1))

_, dryRun, _ := pksLoginRunner.RunArgsForCall(0)
Expect(dryRun).To(BeFalse())
})
})

It("runs the pks login tool with no additional args", func() {
Expect(pksLoginRunner.RunCallCount()).To(Equal(1))

_, _, args := pksLoginRunner.RunArgsForCall(0)
Expect(args).To(BeEmpty())
})

When("running the pks login tool is successful", func() {
BeforeEach(func() {
pksLoginRunner.RunReturns(nil)
})

It("doesn't error", func() {
Expect(err).NotTo(HaveOccurred())
})
})

When("running the pks login tool errors", func() {
BeforeEach(func() {
pksLoginRunner.RunReturns(fmt.Errorf("pks-login-runnner-error"))
})

It("propagates the error", func() {
Expect(err).To(MatchError("pks-login-runnner-error"))
})
})
})
})
32 changes: 27 additions & 5 deletions environment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ type OpsManager struct {
PrivateKey string
}

type PKSApi struct {
Username string
Password string
URL url.URL
}

type Config struct {
Name string
Version version.Version
CFDomain string
AppsDomain string
OpsManager OpsManager
PKSApi PKSApi
PasSubnet string
ServiceSubnet string
AZs []string
Expand All @@ -54,6 +61,11 @@ type environmentReader struct {
Username string `json:"username"`
Password string `json:"password"`
} `json:"ops_manager"`
PKSApi struct {
Username string `json:"uaa_admin_user"`
Password string `json:"uaa_admin_password"`
URL string `json:"url"`
} `json:"pks_api"`
}

func FromFile(path string) (Config, error) {
Expand Down Expand Up @@ -82,16 +94,21 @@ func newLockfile(data environmentReader) (Config, error) {
}
}

parsedURL, err := url.Parse(data.OpsManager.URL)
parsedOpsManagerURL, err := url.Parse(data.OpsManager.URL)
if err != nil {
return Config{}, err
}

ip := net.ParseIP(data.IP)
if ip == nil {
opsManagerIp := net.ParseIP(data.IP)
if opsManagerIp == nil {
return Config{}, fmt.Errorf("Could not parse IP address: %s", data.IP)
}

parsedPKSApiURL, err := url.Parse(data.PKSApi.URL)
if err != nil {
return Config{}, err
}

return Config{
Name: data.Name,
Version: *parsedVersion,
Expand All @@ -103,9 +120,14 @@ func newLockfile(data environmentReader) (Config, error) {
OpsManager: OpsManager{
Username: data.OpsManager.Username,
Password: data.OpsManager.Password,
URL: *parsedURL,
IP: ip,
URL: *parsedOpsManagerURL,
IP: opsManagerIp,
PrivateKey: data.PrivateKey,
},
PKSApi: PKSApi{
Username: data.PKSApi.Username,
Password: data.PKSApi.Password,
URL: *parsedPKSApiURL,
},
}, nil
}
8 changes: 8 additions & 0 deletions environment/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func checkMatchLemon(e Config) {
"PasSubnet": Equal("lemon-pas-subnet"),
"ServiceSubnet": Equal("lemon-services-subnet"),
"AZs": Equal([]string{"us-central1-f", "us-central1-a", "us-central1-c"}),
"PKSApi": MatchFields(IgnoreExtras, Fields{
"Username": Equal("pivotalcf"),
"Password": Equal("fakePassword"),
"URL": Equal(mustParseURL("https://api.pks.lemon-lemon.cf-app.com")),
}),
"OpsManager": MatchAllFields(Fields{
"Username": Equal("pivotalcf"),
"Password": Equal("fakePassword"),
Expand All @@ -77,6 +82,9 @@ func checkMatchReduced(e Config) {
Expect(e).To(MatchFields(IgnoreExtras, Fields{
"Name": Equal("reduced-config"),
"CFDomain": Equal("sys.reduced-config.cf-app.com"),
"PKSApi": MatchFields(IgnoreExtras, Fields{
"URL": Equal(mustParseURL("https://api.pks.reduced-config.cf-app.com")),
}),
"OpsManager": MatchFields(IgnoreExtras, Fields{
"Username": Equal("pivotalcf"),
"Password": Equal("fakePassword"),
Expand Down
5 changes: 5 additions & 0 deletions environment/fixtures/lemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
"ops_manager_public_key": "ubuntu:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDGPKi11B84ogwuGf4Zmq8/VJjvCxgLcpSLD5xuL0FDR1TCA1aeMidb9Pi/DKMfUORry0XbpllgWSbs3YbIEG6FFJEKe68zra/pJSyHH8UE41eULLGWP0ybdAPv5Tvq5D/g1ZtYUnX0jye333akVddM3qh52Dthye0+VKVt4MOH+q2XBLFQ66rhDd6U65MI9YzVpoPVam2WYngGY0a+Am+eo9pww36zk0/osDzLrD57hg3A0v+Otft7l/EiV6elTqLJxHg8BpO0bxWCdBFyJ/i0v+upfrbSgiMAP1QXjZkcSdDeLg0bS5QFX8Nei1/ZjvIHWQHwSUxDfIi67ly+BNmgQHuR2FPFemEkx7EqicV7TWuxfEevMc1edFFkdtzKv1tG8yQEtxPhX6tamz4TBmsu2a5fZ8er47bTI4x7tgCJdNEjhsDVlA1jsqU82i7JLjfXFhbu1hwfBVmKWaaWSeGYMxC8hAGKFXg77Ebo/Nd87urBApmwTOTj5sOFKrOz2BSqiZHYJ/2n8NeH7+CCXgdZQnHrt+u5DyNSpJSorQkNDgQzPunh9TmAVOh2of8gfqryjht6UayWyw1g3S1B1KXgP5RlvfnwL0yhOryLDk3qcpaivsAqFQAKVM6X9KHIbn83rljJxQQ7SmPTJ6TsSKtdZxFlWZklHz4e/smLnclOzQ==\n",
"ops_manager_subnet": "lemon-management-subnet",
"ops_manager_version": "2.0-build.314",
"pks_api": {
"uaa_admin_password": "fakePassword",
"uaa_admin_user": "pivotalcf",
"url": "https://api.pks.lemon-lemon.cf-app.com"
},
"project": "cf-toolsmiths-pool-us-2",
"region": "us-central1",
"service_network_name": "lemon-pcf-network",
Expand Down
3 changes: 3 additions & 0 deletions environment/fixtures/reduced.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "reduced-config",
"pks_api": {
"url": "https://api.pks.reduced-config.cf-app.com"
},
"ops_manager": {
"password": "fakePassword",
"url": "https://pcf.reduced-config.cf-app.com",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/pivotal/hammer
go 1.12

require (
github.com/golang/protobuf v1.3.1 // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/hashicorp/go-version v1.2.0
github.com/jessevdk/go-flags v1.4.0
github.com/kr/pretty v0.1.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
Expand Down
2 changes: 1 addition & 1 deletion integration/cf_login_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var _ = Describe("CF", func() {

Eventually(session).Should(Exit(0))
Eventually(string(session.Err.Contents())).Should(Equal(""))
Eventually(session.Out).Should(Say("Logging in to: https://pcf.manatee.cf-app.com"))
Eventually(session.Out).Should(Say("Logging in to CF at: https://pcf.manatee.cf-app.com"))

output := strings.TrimSuffix(string(session.Out.Contents()), "\n")
lines := strings.Split(output, "\n")
Expand Down
5 changes: 5 additions & 0 deletions integration/fixtures/claim_manatee_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
"ops_manager_public_key": "ubuntu:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDGPKi11B84ogwuGf4Zmq8/VJjvCxgLcpSLD5xuL0FDR1TCA1aeMidb9Pi/DKMfUORry0XbpllgWSbs3YbIEG6FFJEKe68zra/pJSyHH8UE41eULLGWP0ybdAPv5Tvq5D/g1ZtYUnX0jye333akVddM3qh52Dthye0+VKVt4MOH+q2XBLFQ66rhDd6U65MI9YzVpoPVam2WYngGY0a+Am+eo9pww36zk0/osDzLrD57hg3A0v+Otft7l/EiV6elTqLJxHg8BpO0bxWCdBFyJ/i0v+upfrbSgiMAP1QXjZkcSdDeLg0bS5QFX8Nei1/ZjvIHWQHwSUxDfIi67ly+BNmgQHuR2FPFemEkx7EqicV7TWuxfEevMc1edFFkdtzKv1tG8yQEtxPhX6tamz4TBmsu2a5fZ8er47bTI4x7tgCJdNEjhsDVlA1jsqU82i7JLjfXFhbu1hwfBVmKWaaWSeGYMxC8hAGKFXg77Ebo/Nd87urBApmwTOTj5sOFKrOz2BSqiZHYJ/2n8NeH7+CCXgdZQnHrt+u5DyNSpJSorQkNDgQzPunh9TmAVOh2of8gfqryjht6UayWyw1g3S1B1KXgP5RlvfnwL0yhOryLDk3qcpaivsAqFQAKVM6X9KHIbn83rljJxQQ7SmPTJ6TsSKtdZxFlWZklHz4e/smLnclOzQ==\n",
"ops_manager_subnet": "manatee-management-subnet",
"ops_manager_version": "2.0-build.314",
"pks_api": {
"uaa_admin_password": "password ",
"uaa_admin_user": "admin",
"url": "https://api.pks.manatee.cf-app.com"
},
"project": "cf-toolsmiths-pool-us-2",
"region": "us-central1",
"service_network_name": "manatee-pcf-network",
Expand Down
5 changes: 5 additions & 0 deletions integration/fixtures/pks_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
prods="$(om -t https://pcf.manatee.cf-app.com -k -u pivotalcf -p fakePassword curl -s -p /api/v0/staged/products)"
guid="$(echo "$prods" | jq -r '.[] | select(.type == "pivotal-container-service") | .guid')"
creds="$(om -t https://pcf.manatee.cf-app.com -k -u pivotalcf -p fakePassword curl -s -p /api/v0/deployed/products/"$guid"/credentials/.properties.uaa_admin_password)"
pass="$(echo "$creds" | jq -r .credential.value.secret)"
pks login -a https://api.pks.manatee.cf-app.com -u admin -p "$pass" --skip-ssl-validation
Loading

0 comments on commit cd939cc

Please sign in to comment.