Skip to content

Commit 82d9290

Browse files
committed
feat(api): Added GET and LIST commands for CertStoreContainers
1 parent 1a0460a commit 82d9290

File tree

5 files changed

+190
-96
lines changed

5 files changed

+190
-96
lines changed

GNUmakefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ install:
3838
cp ${BINARY} ${INSTALLDIR}
3939
mv ${BINARY} /usr/local/bin/${BINARY}
4040

41+
vendor:
42+
./vendor.sh
4143

4244
test:
4345
go test -i $(TEST) || exit 1
@@ -52,4 +54,4 @@ prerelease:
5254
git tag $(VERSION)
5355
git push origin $(VERSION)
5456

55-
.PHONY: build prerelease release install test fmt
57+
.PHONY: build prerelease release install test fmt vendor

cmd/containers.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
44
package cmd
55

66
import (
7+
"encoding/json"
78
"fmt"
9+
"io/ioutil"
10+
"log"
811

912
"github.com/spf13/cobra"
1013
)
@@ -14,17 +17,14 @@ var containersCmd = &cobra.Command{
1417
Use: "containers",
1518
Short: "Keyfactor CertificateStoreContainer API and utilities.",
1619
Long: `A collections of APIs and utilities for interacting with Keyfactor certificate store containers.`,
17-
Run: func(cmd *cobra.Command, args []string) {
18-
fmt.Println("containers called")
19-
},
2020
}
2121

2222
var containersCreateCmd = &cobra.Command{
2323
Use: "create",
2424
Short: "Create certificate store container.",
2525
Long: `Create certificate store container.`,
2626
Run: func(cmd *cobra.Command, args []string) {
27-
fmt.Println("containers create called")
27+
fmt.Println("Create store containers not implemented.")
2828
},
2929
}
3030

@@ -33,7 +33,20 @@ var containersGetCmd = &cobra.Command{
3333
Short: "Get certificate store container by ID or name.",
3434
Long: `Get certificate store container by ID or name.`,
3535
Run: func(cmd *cobra.Command, args []string) {
36-
fmt.Println("containers get called")
36+
log.SetOutput(ioutil.Discard)
37+
client := cmd.Flag("client").Value.String()
38+
kfClient, _ := initClient()
39+
agents, aErr := kfClient.GetStoreContainer(client)
40+
if aErr != nil {
41+
fmt.Printf("Error, unable to get orchestrator %s. %s\n", client, aErr)
42+
log.Fatalf("Error: %s", aErr)
43+
}
44+
output, jErr := json.Marshal(agents)
45+
if jErr != nil {
46+
fmt.Printf("Error invalid API response from Keyfactor. %s\n", jErr)
47+
log.Fatalf("[ERROR]: %s", jErr)
48+
}
49+
fmt.Printf("%s", output)
3750
},
3851
}
3952

@@ -42,7 +55,7 @@ var containersUpdateCmd = &cobra.Command{
4255
Short: "Update certificate store container by ID or name.",
4356
Long: `Update certificate store container by ID or name.`,
4457
Run: func(cmd *cobra.Command, args []string) {
45-
fmt.Println("containers update called")
58+
fmt.Println("Update store containers not implemented.")
4659
},
4760
}
4861

@@ -51,7 +64,7 @@ var containersDeleteCmd = &cobra.Command{
5164
Short: "Delete certificate store container by ID or name.",
5265
Long: `Delete certificate store container by ID or name.`,
5366
Run: func(cmd *cobra.Command, args []string) {
54-
fmt.Println("containers delete called")
67+
fmt.Println("Delete store containers not implemented.")
5568
},
5669
}
5770

@@ -60,7 +73,19 @@ var containersListCmd = &cobra.Command{
6073
Short: "List certificate store containers.",
6174
Long: `List certificate store containers.`,
6275
Run: func(cmd *cobra.Command, args []string) {
63-
fmt.Println("containers list called")
76+
log.SetOutput(ioutil.Discard)
77+
kfClient, _ := initClient()
78+
agents, aErr := kfClient.GetStoreContainers()
79+
if aErr != nil {
80+
fmt.Printf("Error, unable to list store containers. %s\n", aErr)
81+
log.Fatalf("Error: %s", aErr)
82+
}
83+
output, jErr := json.Marshal(agents)
84+
if jErr != nil {
85+
fmt.Printf("Error invalid API response from Keyfactor. %s\n", jErr)
86+
log.Fatalf("[ERROR]: %s", jErr)
87+
}
88+
fmt.Printf("%s", output)
6489
},
6590
}
6691

@@ -70,11 +95,12 @@ func init() {
7095
containersCmd.AddCommand(containersListCmd)
7196
// GET containers command
7297
containersCmd.AddCommand(containersGetCmd)
98+
containersGetCmd.Flags().StringP("id", "i", "", "ID or name of the cert store container.")
7399
// CREATE containers command
74-
containersCmd.AddCommand(containersCreateCmd)
100+
//containersCmd.AddCommand(containersCreateCmd)
75101
// UPDATE containers command
76-
containersCmd.AddCommand(containersUpdateCmd)
102+
//containersCmd.AddCommand(containersUpdateCmd)
77103
// DELETE containers command
78-
containersCmd.AddCommand(containersDeleteCmd)
104+
//containersCmd.AddCommand(containersDeleteCmd)
79105
// Utility functions
80106
}

cmd/orchs.go

Lines changed: 107 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
22
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3-
43
*/
54
package cmd
65

@@ -16,81 +15,160 @@ import (
1615
// orchsCmd represents the orchs command
1716
var orchsCmd = &cobra.Command{
1817
Use: "orchs",
19-
Short: "Keyfactor agents APIs and utilities.",
18+
Short: "Keyfactor agents/orchestrators APIs and utilities.",
2019
Long: `A collections of APIs and utilities for interacting with Keyfactor orchestrators.`,
2120
}
2221

22+
// getOrchestratorCmd represents the get orchestrator command
2323
var getOrchestratorCmd = &cobra.Command{
2424
Use: "get",
25-
Short: "Get orchestrator by ID or machine/host name.",
26-
Long: `Get orchestrator by ID or machine/host name.`,
25+
Short: "Get orchestrator by machine/client name.",
26+
Long: `Get orchestrator by machine/client name.`,
2727
Run: func(cmd *cobra.Command, args []string) {
28-
fmt.Println("orchestrator get called")
28+
log.SetOutput(ioutil.Discard)
29+
client := cmd.Flag("client").Value.String()
30+
kfClient, _ := initClient()
31+
agents, aErr := kfClient.GetAgent(client)
32+
if aErr != nil {
33+
fmt.Printf("Error, unable to get orchestrator %s. %s\n", client, aErr)
34+
log.Fatalf("Error: %s", aErr)
35+
}
36+
output, jErr := json.Marshal(agents)
37+
if jErr != nil {
38+
fmt.Println("Error invalid API response from Keyfactor.")
39+
log.Fatalf("Error: %s", jErr)
40+
}
41+
fmt.Printf("%s", output)
2942
},
3043
}
3144

45+
// listOrchestratorsCmd represents the list orchestrators command
3246
var approveOrchestratorCmd = &cobra.Command{
3347
Use: "approve",
34-
Short: "Approve orchestrator by ID or machine/host name.",
35-
Long: `Approve orchestrator by ID or machine/host name.`,
48+
Short: "Approve orchestrator by ID or machine/client name.",
49+
Long: `Approve orchestrator by ID or machine/client name.`,
3650
Run: func(cmd *cobra.Command, args []string) {
37-
fmt.Println("orchestrator approve called")
51+
log.SetOutput(ioutil.Discard)
52+
client := cmd.Flag("client").Value.String()
53+
kfClient, cErr := initClient()
54+
if cErr != nil {
55+
fmt.Println("Error, unable to connect to Keyfactor.")
56+
log.Fatalf("Error: %s", cErr)
57+
}
58+
agents, aErr := kfClient.GetAgent(client)
59+
if aErr != nil {
60+
fmt.Printf("Error, unable to get orchestrator %s. %s\n", client, aErr)
61+
log.Fatalf("[ERROR]: %s", aErr)
62+
}
63+
agent := agents[0]
64+
_, aErr = kfClient.ApproveAgent(agent.AgentId)
65+
if aErr != nil {
66+
fmt.Printf("Error, unable to approve orchestrator %s. %s\n", client, aErr)
67+
log.Fatalf("[ERROR]: %s", aErr)
68+
}
69+
fmt.Printf("Orchestrator %s approved.\n", client)
3870
},
3971
}
4072

73+
// disapproveOrchestratorCmd represents the disapprove orchestrator command
4174
var disapproveOrchestratorCmd = &cobra.Command{
4275
Use: "disapprove",
43-
Short: "Disapprove orchestrator by ID or machine/host name.",
44-
Long: `Disapprove orchestrator by ID or machine/host name.`,
76+
Short: "Disapprove orchestrator by ID or machine/client name.",
77+
Long: `Disapprove orchestrator by ID or machine/client name.`,
4578
Run: func(cmd *cobra.Command, args []string) {
46-
fmt.Println("orchestrator disapprove called")
79+
log.SetOutput(ioutil.Discard)
80+
client := cmd.Flag("client").Value.String()
81+
kfClient, cErr := initClient()
82+
if cErr != nil {
83+
fmt.Println("Error, unable to connect to Keyfactor.")
84+
log.Fatalf("Error: %s", cErr)
85+
}
86+
agents, aErr := kfClient.GetAgent(client)
87+
if aErr != nil {
88+
fmt.Printf("Error, unable to get orchestrator %s. %s\n", client, aErr)
89+
log.Fatalf("[ERROR]: %s", aErr)
90+
}
91+
agent := agents[0]
92+
_, aErr = kfClient.DisApproveAgent(agent.AgentId)
93+
if aErr != nil {
94+
fmt.Printf("Error, unable to disapprove orchestrator %s. %s\n", client, aErr)
95+
log.Fatalf("[ERROR]: %s", aErr)
96+
}
97+
fmt.Printf("Orchestrator %s disapproved.\n", client)
4798
},
4899
}
49100

101+
// resetOrchestratorCmd represents the reset orchestrator command
50102
var resetOrchestratorCmd = &cobra.Command{
51103
Use: "reset",
52-
Short: "Reset orchestrator by ID or machine/host name.",
53-
Long: `Reset orchestrator by ID or machine/host name.`,
104+
Short: "Reset orchestrator by ID or machine/client name.",
105+
Long: `Reset orchestrator by ID or machine/client name.`,
54106
Run: func(cmd *cobra.Command, args []string) {
55107
fmt.Println("orchestrator reset called")
56108
},
57109
}
58110

111+
// getLogsOrchestratorCmd represents the get logs orchestrator command
59112
var getLogsOrchestratorCmd = &cobra.Command{
60113
Use: "logs",
61-
Short: "Get orchestrator logs by ID or machine/host name.",
62-
Long: `Get orchestrator logs by ID or machine/host name.`,
114+
Short: "Get orchestrator logs by ID or machine/client name.",
115+
Long: `Get orchestrator logs by ID or machine/client name.`,
63116
Run: func(cmd *cobra.Command, args []string) {
64-
fmt.Println("orchestrator logs called")
117+
log.SetOutput(ioutil.Discard)
118+
client := cmd.Flag("client").Value.String()
119+
kfClient, cErr := initClient()
120+
if cErr != nil {
121+
fmt.Println("Error, unable to connect to Keyfactor.")
122+
log.Fatalf("Error: %s", cErr)
123+
}
124+
agents, aErr := kfClient.GetAgent(client)
125+
if aErr != nil {
126+
fmt.Printf("Error, unable to get logs for orchestrator %s. %s\n", client, aErr)
127+
log.Fatalf("[ERROR]: %s", aErr)
128+
}
129+
agent := agents[0]
130+
_, aErr = kfClient.FetchAgentLogs(agent.AgentId)
131+
if aErr != nil {
132+
fmt.Printf("Error, unable to get logs for orchestrator %s. %s\n", client, aErr)
133+
log.Fatalf("[ERROR]: %s", aErr)
134+
}
135+
fmt.Printf("Fetching logs from %s successful.\n", client)
65136
},
66137
}
67138

139+
// listOrchestratorsCmd represents the list orchestrators command
68140
var listOrchestratorsCmd = &cobra.Command{
69141
Use: "list",
70142
Short: "List orchestrators.",
71143
Long: `Returns a JSON list of Keyfactor orchestrators.`,
72144
Run: func(cmd *cobra.Command, args []string) {
73145
log.SetOutput(ioutil.Discard)
74146
kfClient, _ := initClient()
75-
agents, err := kfClient.GetAgentList()
76-
if err != nil {
77-
log.Printf("Error: %s", err)
147+
agents, aErr := kfClient.GetAgentList()
148+
if aErr != nil {
149+
fmt.Printf("Error, unable to get orchestrators list. %s\n", aErr)
150+
log.Fatalf("Error: %s", aErr)
78151
}
79152
output, jErr := json.Marshal(agents)
80153
if jErr != nil {
81-
log.Printf("Error: %s", jErr)
154+
fmt.Println("Error, unable to get orchestrators list.")
155+
log.Fatalf("Error: %s", jErr)
82156
}
83157
fmt.Printf("%s", output)
84158
},
85159
}
86160

87161
func init() {
162+
var client string
163+
88164
rootCmd.AddCommand(orchsCmd)
89165

90166
// LIST orchestrators command
91167
orchsCmd.AddCommand(listOrchestratorsCmd)
92168
// GET orchestrator command
93169
orchsCmd.AddCommand(getOrchestratorCmd)
170+
getOrchestratorCmd.Flags().StringVarP(&client, "client", "c", "", "Get a specific orchestrator by machine or client name.")
171+
getOrchestratorCmd.MarkFlagRequired("client")
94172
// CREATE orchestrator command TODO: API NOT SUPPORTED
95173
//orchsCmd.AddCommand(createOrchestratorCmd)
96174
// UPDATE orchestrator command TODO: API NOT SUPPORTED
@@ -99,14 +177,23 @@ func init() {
99177
//orchsCmd.AddCommand(deleteOrchestratorCmd)
100178
// APPROVE orchestrator command
101179
orchsCmd.AddCommand(approveOrchestratorCmd)
180+
approveOrchestratorCmd.Flags().StringVarP(&client, "client", "c", "", "Approve a specific orchestrator by machine or client name.")
181+
approveOrchestratorCmd.MarkFlagRequired("client")
102182
// DISAPPROVE orchestrator command
103183
orchsCmd.AddCommand(disapproveOrchestratorCmd)
184+
disapproveOrchestratorCmd.Flags().StringVarP(&client, "client", "c", "", "Disapprove a specific orchestrator by machine or client name.")
185+
disapproveOrchestratorCmd.MarkFlagRequired("client")
104186
// RESET orchestrator command
105187
orchsCmd.AddCommand(resetOrchestratorCmd)
188+
resetOrchestratorCmd.Flags().StringVarP(&client, "client", "c", "", "Reset a specific orchestrator by machine or client name.")
189+
resetOrchestratorCmd.MarkFlagRequired("client")
106190
// GET orchestrator logs command
107191
orchsCmd.AddCommand(getLogsOrchestratorCmd)
192+
getLogsOrchestratorCmd.Flags().StringVarP(&client, "client", "c", "", "Get logs for a specific orchestrator by machine or client name.")
193+
getLogsOrchestratorCmd.MarkFlagRequired("client")
108194
// SET orchestrator auth certificate reenrollment command TODO: Not implemented
109195
//orchsCmd.AddCommand(setOrchestratorAuthCertReenrollCmd)
110196
// Utility commands
111197
//orchsCmd.AddCommand(downloadOrchestrator) TODO: Not implemented
198+
//orchsCmd.AddCommand(installOrchestrator) TODO: Not implemented
112199
}

0 commit comments

Comments
 (0)