Skip to content
This repository has been archived by the owner on Apr 20, 2021. It is now read-only.

Commit

Permalink
Revert "Add experimental support for user tokens"
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwilkie authored Nov 4, 2016
1 parent d00033f commit 4f96c51
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 157 deletions.
66 changes: 11 additions & 55 deletions cmd/wcloud/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,17 @@ func env(key, def string) string {
return def
}

const cliConfigFile = "~/.wcloudconfig"
var (
token = env("SERVICE_TOKEN", "")
baseURL = env("BASE_URL", "https://cloud.weave.works")
)

func usage() {
fmt.Printf(`Usage: wcloud COMMAND ...
fmt.Println(`Usage:
deploy <image>:<version> Deploy image to your configured env
list List recent deployments
config (<filename>) Get (or set) the configured env
logs <deploy> Show lots for the given deployment
Environment Variables:
SERVICE_TOKEN Set the service token to use, overrides %s
BASE_URL Set the deploy to connect to, overrides %s
INSTANCE Set the remote instance id, overrides %s
`,
cliConfigFile,
cliConfigFile,
cliConfigFile,
)
logs <deploy> Show lots for the given deployment`)
}

func main() {
Expand All @@ -62,23 +55,7 @@ func main() {
os.Exit(1)
}

cliConfig, err := loadCLIConfig()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
token := env("SERVICE_TOKEN", cliConfig.ServiceToken)
baseURL := env("BASE_URL", cliConfig.BaseURL)
instance := env("INSTANCE", cliConfig.Instance)
if baseURL == "" {
baseURL = "https://cloud.weave.works"
}

c, err := NewClient(token, baseURL, instance)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
c := NewClient(token, baseURL)

switch os.Args[1] {
case "deploy":
Expand All @@ -98,15 +75,9 @@ func main() {
}
}

func newFlagSet() *flag.FlagSet {
flags := flag.NewFlagSet("", flag.ContinueOnError)
flags.Usage = usage
return flags
}

func deploy(c Client, args []string) {
var (
flags = newFlagSet()
flags = flag.NewFlagSet("", flag.ContinueOnError)
username = flags.String("u", "", "Username to report to deploy service (default with be current user)")
services ArrayFlags
)
Expand Down Expand Up @@ -147,7 +118,7 @@ func deploy(c Client, args []string) {

func list(c Client, args []string) {
var (
flags = newFlagSet()
flags = flag.NewFlagSet("", flag.ContinueOnError)
since = flags.Duration("since", 7*24*time.Hour, "How far back to fetch results")
)
if err := flags.Parse(args); err != nil {
Expand Down Expand Up @@ -180,7 +151,7 @@ func list(c Client, args []string) {

func events(c Client, args []string) {
var (
flags = newFlagSet()
flags = flag.NewFlagSet("", flag.ContinueOnError)
since = flags.Duration("since", 7*24*time.Hour, "How far back to fetch results")
)
if err := flags.Parse(args); err != nil {
Expand Down Expand Up @@ -214,7 +185,7 @@ func loadConfig(filename string) (*Config, error) {
return nil, err
}
}
return &config, err
return &config, nil
}

func config(c Client, args []string) {
Expand Down Expand Up @@ -251,21 +222,6 @@ func config(c Client, args []string) {
}
}

func loadCLIConfig() (CLIConfig, error) {
buf, err := ioutil.ReadFile(cliConfigFile)
if err != nil {
if os.IsNotExist(err) {
return CLIConfig{}, nil
}
return CLIConfig{}, err
}
var cliConfig CLIConfig
if err := yaml.Unmarshal(buf, &cliConfig); err != nil {
return CLIConfig{}, err
}
return cliConfig, err
}

func logs(c Client, args []string) {
if len(args) != 1 {
usage()
Expand Down
66 changes: 7 additions & 59 deletions cmd/wcloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,76 +11,24 @@ import (

// Client for the deployment service
type Client struct {
token string
baseURL string
instance string // TODO: Use this in urls
authType string
token string
baseURL string
}

// NewClient makes a new Client
func NewClient(token, baseURL, instance string) (Client, error) {
c := Client{
token: token,
baseURL: baseURL,
instance: instance,
authType: "Scope-User",
func NewClient(token, baseURL string) Client {
return Client{
token: token,
baseURL: baseURL,
}

// TODO: Detect the type of token and get the instance id separately
if instance == "" {
err := c.getInstanceID()
if err == ErrUnauthorized {
c.authType = "Scope-Probe"
err = c.getInstanceID()
}
if err != nil {
return Client{}, err
}
}

if c.authType == "Scope-User" {
c.baseURL = fmt.Sprintf("%s/api/app/%s", c.baseURL, c.instance)
}

return c, nil
}

func (c *Client) getInstanceID() error {
// User did not provide an instance, check if we can auto-detect only 1 instance
req, err := http.NewRequest("GET", c.baseURL+"/api/users/lookup", nil)
if err != nil {
return err
}
req.Header.Add("Authorization", fmt.Sprintf("%s token=%s", c.authType, c.token))
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if res.StatusCode == http.StatusUnauthorized {
return ErrUnauthorized
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("Error initializing client: %d\n", res.StatusCode)
}

defer res.Body.Close()
var lookup lookupView
if err := json.NewDecoder(res.Body).Decode(&lookup); err != nil {
return err
}
if len(lookup.Instances) != 1 {
return ErrMultipleInstances(lookup)
}
c.instance = lookup.Instances[0].ExternalID
return nil
}

func (c Client) newRequest(method, path string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, c.baseURL+path, body)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("%s token=%s", c.authType, c.token))
req.Header.Add("Authorization", fmt.Sprintf("Scope-Probe token=%s", c.token))
return req, nil
}

Expand Down
25 changes: 0 additions & 25 deletions cmd/wcloud/errors.go

This file was deleted.

18 changes: 0 additions & 18 deletions cmd/wcloud/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,3 @@ type NotificationConfig struct {
MessageTemplate string `json:"message_template" yaml:"message_template"`
ApplyMessageTemplate string `json:"apply_message_template" yaml:"apply_message_template"`
}

// CLIConfig is used to store local wcloud cli configs
type CLIConfig struct {
ServiceToken string `yaml:"service_token"`
BaseURL string `yaml:"base_url"`
Instance string `yaml:"instance,omitempty"`
}

// lookupView is returned from /api/users/lookup. Only includes the fields we care about.
type lookupView struct {
Instances []Instance `json:"organizations,omitempty"`
}

// Instance is a helper for data returned as part of the lookupView.
type Instance struct {
ExternalID string `json:"id"`
Name string `json:"name"`
}

0 comments on commit 4f96c51

Please sign in to comment.