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 support for teleport and improve doc #19

Merged
merged 1 commit into from
Mar 18, 2024
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,20 @@ How to run integration test:
./test_final_exec.sh
```

## How to use behind teleport
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only work if people have like us a console instance declared as teleport application, not sure that there are many

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose we remove this section when we make this README.md public

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still can be useful to know that you can use cert and key to access a console behind a secured proxy


First login to your teleport proxy, for example:
```
tsh login --proxy=teleport-01.prd.tooling.cdkt.dev --auth=github
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't give our teleport server url to in a repo that will end up public
Just say "login to your teleport server"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess we will clean the doc when we make it public no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't forget ...
I've created a linear ticket for that https://linear.app/conduktor/issue/CONS-872/go-public

```

```
conduktor get application --cert $(tsh apps config --format=cert) --key $(tsh apps config --format=key)
```

Or:
```
export CDK_CERT=$(tsh apps config --format=cert)
export CDK_KEY=$(tsh apps config --format=key)
conduktor get application
```
18 changes: 14 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"crypto/tls"
"encoding/json"
"fmt"
"os"
Expand All @@ -17,15 +18,16 @@ type Client struct {
client *resty.Client
}

func Make(token string, baseUrl string, debug bool) Client {
func Make(token string, baseUrl string, debug bool, key, cert string) Client {
certificate, _ := tls.LoadX509KeyPair(cert, key)
return Client{
token: token,
baseUrl: baseUrl,
client: resty.New().SetDebug(debug).SetHeader("Authorization", "Bearer "+token),
client: resty.New().SetDebug(debug).SetHeader("Authorization", "Bearer "+token).SetCertificates(certificate),
}
}

func MakeFromEnv(debug bool) Client {
func MakeFromEnv(debug bool, key, cert string) Client {
token := os.Getenv("CDK_TOKEN")
if token == "" {
fmt.Fprintln(os.Stderr, "Please set CDK_TOKEN")
Expand All @@ -36,8 +38,16 @@ func MakeFromEnv(debug bool) Client {
fmt.Fprintln(os.Stderr, "Please set CDK_BASE_URL")
os.Exit(2)
}
finalKey := key
finalCert := cert
if finalKey == "" {
finalKey = os.Getenv("CDK_KEY")
}
if finalCert == "" {
finalCert = os.Getenv("CDK_CERT")
}

return Make(token, baseUrl, debug)
return Make(token, baseUrl, debug, finalKey, finalCert)
}

type UpsertResponse struct {
Expand Down
22 changes: 11 additions & 11 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestApplyShouldWork(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
token := "aToken"
client := Make(token, baseUrl, false)
client := Make(token, baseUrl, false, "", "")
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestApplyWithDryModeShouldWork(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
token := "aToken"
client := Make(token, baseUrl, false)
client := Make(token, baseUrl, false, "", "")
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestApplyShouldFailIfNo2xx(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
token := "aToken"
client := Make(token, baseUrl, false)
client := Make(token, baseUrl, false, "", "")
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestGetShouldWork(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
token := "aToken"
client := Make(token, baseUrl, false)
client := Make(token, baseUrl, false, "", "")
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
Expand All @@ -143,7 +143,7 @@ func TestGetShouldApplyCaseTransformation(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
token := "aToken"
client := Make(token, baseUrl, false)
client := Make(token, baseUrl, false, "", "")
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
Expand All @@ -170,7 +170,7 @@ func TestGetShouldKeepCase(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
token := "aToken"
client := Make(token, baseUrl, false)
client := Make(token, baseUrl, false, "", "")
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
Expand All @@ -197,7 +197,7 @@ func TestGetShouldFailIfN2xx(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
token := "aToken"
client := Make(token, baseUrl, false)
client := Make(token, baseUrl, false, "", "")
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
Expand All @@ -224,7 +224,7 @@ func TestDescribeShouldWork(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
token := "aToken"
client := Make(token, baseUrl, false)
client := Make(token, baseUrl, false, "", "")
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
Expand All @@ -251,7 +251,7 @@ func TestDescribeShouldFailIfNo2xx(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
token := "aToken"
client := Make(token, baseUrl, false)
client := Make(token, baseUrl, false, "", "")
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
Expand All @@ -278,7 +278,7 @@ func TestDeleteShouldWork(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
token := "aToken"
client := Make(token, baseUrl, false)
client := Make(token, baseUrl, false, "", "")
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
Expand All @@ -304,7 +304,7 @@ func TestDeleteShouldFailOnNot2XX(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
token := "aToken"
client := Make(token, baseUrl, false)
client := Make(token, baseUrl, false, "", "")
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var applyCmd = &cobra.Command{
}
resources = append(resources, r...)
}
client := client.MakeFromEnv(*debug)
client := client.MakeFromEnv(*debug, *key, *cert)
for _, resource := range resources {
upsertResult, err := client.Apply(&resource, *dryRun)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var deleteCmd = &cobra.Command{
Long: ``,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
client := client.MakeFromEnv(*debug)
client := client.MakeFromEnv(*debug, *key, *cert)
err := client.Delete(args[0], args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
Expand Down
10 changes: 7 additions & 3 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import (
var getCmd = &cobra.Command{
Use: "get kind [name]",
Short: "get resource of a given kind",
Long: ``,
Args: cobra.MatchAll(cobra.MinimumNArgs(1), cobra.MaximumNArgs(2)),
Long: `If name not provided it will list all resource. For example:
conduktor get application
will list all applications. Whereas:
conduktor get application myapp
will describe the application myapp`,
Args: cobra.MatchAll(cobra.MinimumNArgs(1), cobra.MaximumNArgs(2)),
Run: func(cmd *cobra.Command, args []string) {
client := client.MakeFromEnv(*debug)
client := client.MakeFromEnv(*debug, *key, *cert)
var err error
if len(args) == 1 {
err = client.Get(args[0])
Expand Down
7 changes: 6 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import (
)

var debug *bool
var key *string
var cert *string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "conduktor",
Short: "command line tools for conduktor",
Long: ``,
Long: `You need to define the CDK_TOKEN and CDK_BASE_URL environment variables to use this tool.
You can also use the CDK_KEY,CDK_CERT instead of --key and --cert flags to use a certificate for tls authentication.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Expand All @@ -32,4 +35,6 @@ func Execute() {

func init() {
debug = rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Show more information for debugging")
key = rootCmd.PersistentFlags().String("key", "", "Set pem key for certificate authentication (useful for teleport)")
cert = rootCmd.PersistentFlags().String("cert", "", "Set pem cert for certificate authentication (useful for teleport)")
}
Loading