-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add client creation and subscritpion
Resolves #3 Signed-off-by: Jorge Alarcon Ochoa <[email protected]>
- Loading branch information
1 parent
616cb4f
commit 12689fb
Showing
16 changed files
with
415 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
tacc-keys/.tacc-keys.json | ||
tacc-keys/.tacc-keys.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Oops, something went wrong.