Skip to content

Commit

Permalink
Add client creation and subscritpion
Browse files Browse the repository at this point in the history
Resolves #3

Signed-off-by: Jorge Alarcon Ochoa <[email protected]>
  • Loading branch information
alejandrox1 committed Oct 27, 2018
1 parent 616cb4f commit 12689fb
Show file tree
Hide file tree
Showing 16 changed files with 415 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
tacc-keys/.tacc-keys.json
tacc-keys/.tacc-keys.yaml
31 changes: 9 additions & 22 deletions tacc-keys/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
## tacc-keys
# Oficial client for TACC's keys service

Client for TACC's public ssh keys service
Hello, if you have found yourself here then you are probably in need of a
client for TACC's public ssh keys service.

### Synopsis
If this is so, then you have found the perfect tool!
This client will allow you to interact with the keys service by listing keys,
creating public and private key pairs, posting public keys, deleting them,
and managing the oauth client you need to interact with TACC.

Client for TACC's public ssh keys service.

It allows you to create, list, publish, and delete public ssh keys from the
keys service

### Options

```
--config string config file (default is $HOME/.tacc-keys.yaml)
-h, --help help for tacc-keys
```

### SEE ALSO

* [tacc-keys completion](tacc-keys_completion.md) - Generate bash completion scripts and documentation
* [tacc-keys delete](tacc-keys_delete.md) - Delete a public ssh key from TACC's keys service
* [tacc-keys docs](tacc-keys_docs.md) - Generate markdown documentation
* [tacc-keys list](tacc-keys_list.md) - List all public ssh keys registered to a user
* [tacc-keys post](tacc-keys_post.md) - Post a public ssh key to TACC's keys service
To see how you can get started,
[read the short documentation for tacc-keys](./docs/tacc-keys.md).
10 changes: 6 additions & 4 deletions tacc-keys/cmd/refresh_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd
import (
"fmt"
"os"
"strconv"
"time"

"github.com/TACC-Cloud/ssh-keys-client/tacc-keys/tacc-services"
Expand All @@ -36,13 +37,14 @@ func refreshTokenPair() error {
now := time.Now().Unix() - 100
if (createdAt + expiresIn) < now {
fmt.Fprintln(os.Stderr, "Refreshing token...")
newTokens, err := services.RefreshToken(baseURL, refreshToken, apiKey, apiSecret)
access, refresh, err := services.RefreshToken(
baseURL, refreshToken, apiKey, apiSecret)
if err != nil {
return err
}
viper.Set("access_token", newTokens.AccessToken)
viper.Set("refresh_token", newTokens.RefreshToken)
viper.Set("created_at", newTokens.CreatedAt)
viper.Set("access_token", access)
viper.Set("refresh_token", refresh)
viper.Set("created_at", strconv.FormatInt(time.Now().Unix(), 10))
viper.WriteConfig()
}

Expand Down
9 changes: 6 additions & 3 deletions tacc-keys/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ var rootCmd = &cobra.Command{
Use: "tacc-keys",
Short: "Client for TACC's public ssh keys service",
Long: `Client for TACC's public ssh keys service.
It allows you to create, list, publish, and delete public ssh keys from the
keys service`,
It allows you to create, list, publish, and delete public ssh keys from the
keys service.
To use this tool, you'll need a configuration file, ".tacc-keys.(yaml|json|toml)".
You can use the "curent.json" file created by TACC-Cloud/agave-cli.
`,
}

// Execute root command.
Expand Down
95 changes: 95 additions & 0 deletions tacc-keys/cmd/subscribe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
//
// Licensed 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 cmd

import (
"fmt"
"os"
"strconv"
"time"

"github.com/TACC-Cloud/ssh-keys-client/tacc-keys/tacc-services"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
name string
description string
)

// subscribeCmd represents the subscribe command
var subscribeCmd = &cobra.Command{
Use: "subscribe",
Short: "Create an oauth client and subscribe to TACC's keys service",
Long: `Create a TACC oauth client, obtain an access token, and subscribe the
client to the keys service.
This will also generate a config file if one doesn't already exist.`,
Run: func(cmd *cobra.Command, args []string) {
// Set name to hostname if not defined.
if name == "" {
var err error
name, err = os.Hostname()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
baseURL := "https://api.tacc.utexas.edu"

// Ge user credentials.
username, password, err := services.Credentials()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Create an oauth client.
key, secret, err := services.CreateClient(
baseURL, name, description, username, password)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Subscribe client to TACC's keys service.
err = services.SubscribeClient(baseURL, name, username, password)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

access, refresh, err := services.GetTokens(
baseURL, key, secret, username, password)

// Save configurations.
viper.Set("apikey", key)
viper.Set("apisecret", secret)
viper.Set("baseurl", baseURL)
viper.Set("username", username)
viper.Set("access_token", access)
viper.Set("refresh_token", refresh)
viper.Set("created_at", strconv.FormatInt(time.Now().Unix(), 10))
viper.WriteConfigAs(".tacc-keys.yaml")
},
}

func init() {
rootCmd.AddCommand(subscribeCmd)

subscribeCmd.Flags().StringVarP(&name, "name", "n", "", "Name of aouth client")
subscribeCmd.Flags().StringVarP(
&description, "description", "d", "", "Oauth lient description")
}
31 changes: 31 additions & 0 deletions tacc-keys/docs/tacc-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## tacc-keys

Client for TACC's public ssh keys service

### Synopsis

Client for TACC's public ssh keys service.
It allows you to create, list, publish, and delete public ssh keys from the
keys service.

To use this tool, you'll need a configuration file, ".tacc-keys.(yaml|json|toml)".
You can use the "curent.json" file created by TACC-Cloud/agave-cli.


### Options

```
--config string config file (default is $HOME/.tacc-keys.yaml)
-h, --help help for tacc-keys
```

### SEE ALSO

* [tacc-keys completion](tacc-keys_completion.md) - Generate bash completion scripts and documentation
* [tacc-keys delete](tacc-keys_delete.md) - Delete a public ssh key from TACC's keys service
* [tacc-keys docs](tacc-keys_docs.md) - Generate markdown documentation
* [tacc-keys list](tacc-keys_list.md) - List all public ssh keys registered to a user
* [tacc-keys post](tacc-keys_post.md) - Post a public ssh key to TACC's keys service
* [tacc-keys subscribe](tacc-keys_subscribe.md) - Create an oauth client and subscribe to TACC's keys service

###### Auto generated by spf13/cobra on 27-Oct-2018
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ tacc-keys completion [flags]

### SEE ALSO

* [tacc-keys](README.md) - Client for TACC's public ssh keys service
* [tacc-keys](tacc-keys.md) - Client for TACC's public ssh keys service

###### Auto generated by spf13/cobra on 27-Oct-2018
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ tacc-keys delete [key ID] [flags]

### SEE ALSO

* [tacc-keys](README.md) - Client for TACC's public ssh keys service
* [tacc-keys](tacc-keys.md) - Client for TACC's public ssh keys service

###### Auto generated by spf13/cobra on 27-Oct-2018
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ tacc-keys docs [flags]

### SEE ALSO

* [tacc-keys](README.md) - Client for TACC's public ssh keys service
* [tacc-keys](tacc-keys.md) - Client for TACC's public ssh keys service

###### Auto generated by spf13/cobra on 27-Oct-2018
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ tacc-keys list [username] [flags]

### SEE ALSO

* [tacc-keys](README.md) - Client for TACC's public ssh keys service
* [tacc-keys](tacc-keys.md) - Client for TACC's public ssh keys service

###### Auto generated by spf13/cobra on 27-Oct-2018
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ tacc-keys post [username] [flags]

### SEE ALSO

* [tacc-keys](README.md) - Client for TACC's public ssh keys service
* [tacc-keys](tacc-keys.md) - Client for TACC's public ssh keys service

###### Auto generated by spf13/cobra on 27-Oct-2018
33 changes: 33 additions & 0 deletions tacc-keys/docs/tacc-keys_subscribe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## tacc-keys subscribe

Create an oauth client and subscribe to TACC's keys service

### Synopsis

Create a TACC oauth client, obtain an access token, and subscribe the
client to the keys service.
This will also generate a config file if one doesn't already exist.

```
tacc-keys subscribe [flags]
```

### Options

```
-d, --description string Oauth lient description
-h, --help help for subscribe
-n, --name string Name of aouth client
```

### Options inherited from parent commands

```
--config string config file (default is $HOME/.tacc-keys.yaml)
```

### SEE ALSO

* [tacc-keys](tacc-keys.md) - Client for TACC's public ssh keys service

###### Auto generated by spf13/cobra on 27-Oct-2018
31 changes: 31 additions & 0 deletions tacc-keys/tacc-services/credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package services

import (
"bufio"
"fmt"
"os"
"strings"
"syscall"

"golang.org/x/crypto/ssh/terminal"
)

// Credentials ask for username and password at runtime.
func Credentials() (string, string, error) {
reader := bufio.NewReader(os.Stdin)

fmt.Print("Username: ")
username, err := reader.ReadString('\n')
if err != nil {
return "", "", err
}

fmt.Print("Password: ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", "", err
}
password := string(bytePassword)

return strings.TrimSpace(username), strings.TrimSpace(password), nil
}
72 changes: 72 additions & 0 deletions tacc-keys/tacc-services/oauth_clients.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package services

import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)

type clientResponse struct {
Result clientData `json:"result"`
}

type clientData struct {
APIKey string `json:"consumerKey"`
APISecret string `json:"consumerSecret"`
}

// CreateClient creates a TACC oauth client with the provided name and
// description.
func CreateClient(baseURL, name, description, username, password string) (string, string, error) {
// Oauth clients endpoint.
clientEndpoint := baseURL + "/clients/v2"

// Request data.
v := url.Values{}
v.Set("clientName", name)
v.Set("tier", "Unlimited")
v.Set("description", description)
v.Set("callbackUrl", "")
data := v.Encode()
// Form request.
req, err := http.NewRequest("POST", clientEndpoint, strings.NewReader(data))
if err != nil {
return "", "", err
}

// Set headers.
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

// Basic http authentication.
req.SetBasicAuth(username, password)

// Create http client.
client := &http.Client{
Timeout: time.Second * 5,
}
// Make request.
resp, err := client.Do(req)
if err != nil {
return "", "", err
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusCreated {
var client clientResponse
if err := json.NewDecoder(resp.Body).Decode(&client); err != nil {
return "", "", err
}

return client.Result.APIKey, client.Result.APISecret, nil
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", "", err
}
return "", "", errors.New(string(body))
}
Loading

0 comments on commit 12689fb

Please sign in to comment.