Esempio n. 1
0
// Auth returns a valid Auth object for access to openstack services, or
// an error if the authentication couldn't be resolved.
func (c *AccessConfig) Auth() (gophercloud.AccessProvider, error) {
	username := c.Username
	password := c.Password
	project := c.Project
	provider := c.Provider

	if username == "" {
		username = os.Getenv("SDK_USERNAME")
	}
	if password == "" {
		password = os.Getenv("SDK_PASSWORD")
	}
	if project == "" {
		project = os.Getenv("SDK_PROJECT")
	}
	if provider == "" {
		provider = os.Getenv("SDK_PROVIDER")
	}

	authoptions := gophercloud.AuthOptions{
		Username:    username,
		Password:    password,
		AllowReauth: true,
	}

	if project != "" {
		authoptions.TenantName = project
	}

	return gophercloud.Authenticate(provider, authoptions)
}
func main() {
	provider, authOptions, err := osutil.AuthOptions()
	if err != nil {
		panic(err)
	}
	_, err = gophercloud.Authenticate(provider, authOptions)
	if err != nil {
		panic(err)
	}
}
Esempio n. 3
0
func GetClient(project string, region string, cache_path string) (Client, error) {
	c := Client{}
	cache, err := config.LoadCredCache(cache_path)
	if err != nil {
		fmt.Println("unable to get cache")
		return c, err
	}
	c.cache = cache
	if region == "" {
		region = defaultRegion
	}
	c.region = region
	if c.cache.Rackspace.User == "" || c.cache.Rackspace.APIKey == "" {
		var rack_user string
		var rack_pass string
		fmt.Printf("rackspace user: "******"%s", &rack_user)
		if err != nil {
			return c, err
		}
		fmt.Printf("rackspace api key: ")
		_, err = fmt.Scanf("%s", &rack_pass)
		if err != nil {
			return c, err
		}
		c.cache.Rackspace.User = rack_user
		c.cache.Rackspace.APIKey = rack_pass
		c.cache.Save()
		if err != nil {
			return c, err
		}
	}
	ao := gophercloud.AuthOptions{
		Username:    c.cache.Rackspace.User,
		ApiKey:      c.cache.Rackspace.APIKey,
		AllowReauth: true,
	}
	access, err := gophercloud.Authenticate("rackspace-us", ao)
	if err != nil {
		c.cache.Rackspace.User = ""
		c.cache.Rackspace.APIKey = ""
		c.cache.Save()
		return c, err
	}
	ac, err := gophercloud.PopulateApi("rackspace")
	if err != nil {
		return c, err
	}
	client, err := gophercloud.ServersApi(access, ac)
	if err != nil {
		return c, err
	}
	c.client = client
	return c, nil
}
Esempio n. 4
0
// Auth returns a valid Auth object for access to openstack services, or
// an error if the authentication couldn't be resolved.
func (c *AccessConfig) Auth() (gophercloud.AccessProvider, error) {
	c.Username = common.ChooseString(c.Username, os.Getenv("SDK_USERNAME"), os.Getenv("OS_USERNAME"))
	c.Password = common.ChooseString(c.Password, os.Getenv("SDK_PASSWORD"), os.Getenv("OS_PASSWORD"))
	c.ApiKey = common.ChooseString(c.ApiKey, os.Getenv("SDK_API_KEY"))
	c.Project = common.ChooseString(c.Project, os.Getenv("SDK_PROJECT"), os.Getenv("OS_TENANT_NAME"))
	c.Provider = common.ChooseString(c.Provider, os.Getenv("SDK_PROVIDER"), os.Getenv("OS_AUTH_URL"))
	c.RawRegion = common.ChooseString(c.RawRegion, os.Getenv("SDK_REGION"), os.Getenv("OS_REGION_NAME"))
	c.TenantId = common.ChooseString(c.TenantId, os.Getenv("OS_TENANT_ID"))

	// OpenStack's auto-generated openrc.sh files do not append the suffix
	// /tokens to the authentication URL. This ensures it is present when
	// specifying the URL.
	if strings.Contains(c.Provider, "://") && !strings.HasSuffix(c.Provider, "/tokens") {
		c.Provider += "/tokens"
	}

	authoptions := gophercloud.AuthOptions{
		AllowReauth: true,

		ApiKey:     c.ApiKey,
		TenantId:   c.TenantId,
		TenantName: c.Project,
		Username:   c.Username,
		Password:   c.Password,
	}

	default_transport := &http.Transport{}

	if c.Insecure {
		cfg := new(tls.Config)
		cfg.InsecureSkipVerify = true
		default_transport.TLSClientConfig = cfg
	}

	// For corporate networks it may be the case where we want our API calls
	// to be sent through a separate HTTP proxy than external traffic.
	if c.ProxyUrl != "" {
		url, err := url.Parse(c.ProxyUrl)
		if err != nil {
			return nil, err
		}

		// The gophercloud.Context has a UseCustomClient method which
		// would allow us to override with a new instance of http.Client.
		default_transport.Proxy = http.ProxyURL(url)
	}

	if c.Insecure || c.ProxyUrl != "" {
		http.DefaultTransport = default_transport
	}

	return gophercloud.Authenticate(c.Provider, authoptions)
}
Esempio n. 5
0
func newOpenStack(cfg Config) (*OpenStack, error) {
	os := OpenStack{
		provider: cfg.Global.AuthUrl,
		authOpt:  cfg.toAuthOptions(),
		region:   cfg.Global.Region,
	}

	access, err := gophercloud.Authenticate(os.provider, os.authOpt)
	os.access = access

	return &os, err
}
func main() {
	provider, username, password, _ := getCredentials()

	_, err := gophercloud.Authenticate(
		provider,
		gophercloud.AuthOptions{
			Username: username,
			Password: password,
		},
	)
	if err != nil {
		panic(err)
	}
}
Esempio n. 7
0
func (rax *RaxCloud) createGopherCtx() (err error) {
	var (
		provider    string
		authOptions gophercloud.AuthOptions
		apiCriteria gophercloud.ApiCriteria
		access      *gophercloud.Access
		csp         gophercloud.CloudServersProvider
	)

	// Create our auth options set by the user's environment
	provider, authOptions, err = getAuthOptions()
	if err != nil {
		return err
	}

	// Set our API criteria
	apiCriteria, err = gophercloud.PopulateApi("rackspace-us")
	if err != nil {
		return err
	}

	// Set the region
	apiCriteria.Type = "compute"
	apiCriteria.Region = os.Getenv("OS_REGION_NAME")

	if rax.regionOverride != "" {
		log.Printf("Overriding region set in env with %s", rax.regionOverride)
		apiCriteria.Region = rax.regionOverride
	}

	if apiCriteria.Region == "" {
		return fmt.Errorf("No region set. Please set the OS_REGION_NAME environment variable or use the --region flag.")
	}

	// Attempt to authenticate
	access, err = gophercloud.Authenticate(provider, authOptions)
	if err != nil {
		fmt.Printf("ERROR: %v\n\n\n", err)
		return err
	}

	// Get a CSP ref!
	csp, err = gophercloud.ServersApi(access, apiCriteria)
	if err != nil {
		return err
	}

	rax.gopher = csp
	return nil
}
Esempio n. 8
0
func withAPIKeyIdentity(ar bool, f func(gophercloud.AccessProvider)) {
	provider, username, _, apiKey := getCredentials()
	acc, err := gophercloud.Authenticate(
		provider,
		gophercloud.AuthOptions{
			Username:    username,
			ApiKey:      apiKey,
			AllowReauth: ar,
		},
	)
	if err != nil {
		panic(err)
	}

	f(acc)
}
Esempio n. 9
0
func withPasswordIdentity(ar bool, f func(gophercloud.AccessProvider)) {
	provider, username, password, _ := getCredentials()
	acc, err := gophercloud.Authenticate(
		provider,
		gophercloud.AuthOptions{
			Username:    username,
			Password:    password,
			AllowReauth: ar,
		},
	)
	if err != nil {
		panic(err)
	}

	f(acc)
}
Esempio n. 10
0
// Client() returns a new client for accessing openstack.
//
func (c *Config) Client() (*OpenstackClient, error) {

	if v := os.Getenv("OS_AUTH_URL"); v != "" {
		c.Auth = v
	}
	if v := os.Getenv("OS_USERNAME"); v != "" {
		c.User = v
	}
	if v := os.Getenv("OS_PASSWORD"); v != "" {
		c.Password = v
	}
	if v := os.Getenv("OS_TENANT_ID"); v != "" {
		c.TenantId = v
	}
	if v := os.Getenv("OS_TENANT_NAME"); v != "" {
		c.TenantName = v
	}

	// OpenStack's auto-generated openrc.sh files do not append the suffix
	// /tokens to the authentication URL. This ensures it is present when
	// specifying the URL.
	if strings.Contains(c.Auth, "://") && !strings.HasSuffix(c.Auth, "/tokens") {
		c.Auth += "/tokens"
	}

	accessProvider, err := gophercloud.Authenticate(
		c.Auth,
		gophercloud.AuthOptions{
			ApiKey:     c.ApiKey,
			Username:   c.User,
			Password:   c.Password,
			TenantName: c.TenantName,
			TenantId:   c.TenantId,
		},
	)

	if err != nil {
		return nil, err
	}

	client := &OpenstackClient{c, accessProvider}

	log.Printf("[INFO] Openstack Client configured for user %s", client.Config.User)

	return client, nil
}
Esempio n. 11
0
// Auth returns a valid Auth object for access to openstack services, or
// an error if the authentication couldn't be resolved.
func (c *AccessConfig) Auth() (gophercloud.AccessProvider, error) {
	username := c.Username
	password := c.Password
	project := c.Project
	provider := c.Provider
	proxy := c.ProxyUrl

	if username == "" {
		username = os.Getenv("SDK_USERNAME")
	}
	if password == "" {
		password = os.Getenv("SDK_PASSWORD")
	}
	if project == "" {
		project = os.Getenv("SDK_PROJECT")
	}
	if provider == "" {
		provider = os.Getenv("SDK_PROVIDER")
	}

	authoptions := gophercloud.AuthOptions{
		Username:    username,
		Password:    password,
		AllowReauth: true,
	}

	if project != "" {
		authoptions.TenantName = project
	}

	// For corporate networks it may be the case where we want our API calls
	// to be sent through a separate HTTP proxy than external traffic.
	if proxy != "" {
		url, err := url.Parse(proxy)
		if err != nil {
			return nil, err
		}

		// The gophercloud.Context has a UseCustomClient method which
		// would allow us to override with a new instance of http.Client.
		http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(url)}
	}

	return gophercloud.Authenticate(provider, authoptions)
}
Esempio n. 12
0
func main() {
	provider, username, _, apiKey := getCredentials()

	if !strings.Contains(provider, "rackspace") {
		fmt.Fprintf(os.Stdout, "Skipping test because provider doesn't support API_KEYs\n")
		return
	}

	_, err := gophercloud.Authenticate(
		provider,
		gophercloud.AuthOptions{
			Username: username,
			ApiKey:   apiKey,
		},
	)
	if err != nil {
		panic(err)
	}
}
Esempio n. 13
0
func NewRackspaceFromUrl(u *url.URL) (*Rackspace, error) {
	var port = DEFAULT_PORT
	var region = DEFAULT_REGION
	var protocol = DEFAULT_PROTOCOL
	var username = ""
	var apiKey = ""

	if os.Getenv("OS_REGION_NAME") != "" {
		region = strings.ToLower(os.Getenv("OS_REGION_NAME"))
	}

	qs := u.Query()

	if qs.Get("port") != "" {
		port = qs.Get("port")
	}

	if qs.Get("protocol") != "" {
		protocol = qs.Get("protocol")
	}

	if qs.Get("region") != "" {
		region = strings.ToLower(qs.Get("region"))
	}

	if os.Getenv("OS_USERNAME") != "" {
		username = os.Getenv("OS_USERNAME")
	}

	if os.Getenv("OS_PASSWORD") != "" {
		apiKey = os.Getenv("OS_PASSWORD")
	}

	if username == "" && u.User != nil {
		username = u.User.Username()
	} else if username == "" && u.User == nil {
		return nil, fmt.Errorf("Missing Username for Rackspace provider.")
	}

	if apiKey == "" && u.User != nil {
		var ok bool
		apiKey, ok = u.User.Password()
		if !ok {
			return nil, fmt.Errorf("Missing API Key for Rackspace provider.")
		}
	}

	auth := gophercloud.AuthOptions{
		Username:    username,
		ApiKey:      apiKey,
		AllowReauth: true}

	var identityRegion = "rackspace-us"

	if region == "lon" {
		identityRegion = "rackspace-uk"
	}

	ap, err := gophercloud.Authenticate(identityRegion, auth)

	if err != nil {
		return nil, err
	}

	r := &Rackspace{accessProvider: ap,
		region:   region,
		port:     port,
		protocol: protocol,
		backoff:  timeutils.NewBackoffTimer(30.0, 1800.0),
	}

	err = r.UpdateCache()

	if err != nil {
		return nil, err
	}

	go r.Watch()

	return r, nil
}
Esempio n. 14
0
func New(authURL, providerName string, credential, builder map[string]interface{}) (*Openstack, error) {
	// OpenStack's auto-generated openrc.sh files do not append the suffix
	// /tokens to the authentication URL. This ensures it is present when
	// specifying the URL.
	if strings.Contains(authURL, "://") && !strings.HasSuffix(authURL, "/tokens") {
		authURL += "/tokens"
	}

	o := &Openstack{
		AuthURL:  authURL,
		Provider: providerName,
	}

	// Credentials
	if err := mapstructure.Decode(credential, &o.Creds); err != nil {
		return nil, err
	}

	// Builder data
	if err := mapstructure.Decode(builder, &o.Builder); err != nil {
		return nil, err
	}

	if o.Creds.Username == "" {
		return nil, errors.New("Username is not set")
	}

	if o.Creds.Password == "" && o.Creds.ApiKey == "" {
		return nil, errors.New("Password/ApiKey is not set")
	}

	authoptions := gophercloud.AuthOptions{
		AllowReauth: true,
		ApiKey:      o.Creds.ApiKey,
		TenantId:    o.Creds.TenantId,
		TenantName:  o.Creds.TenantName,
		Username:    o.Creds.Username,
		Password:    o.Creds.Password,
	}

	access, err := gophercloud.Authenticate(authURL, authoptions)
	if err != nil {
		return nil, err
	}

	//fetches the api requisites from gophercloud for the appropriate
	//openstack variant
	api, err := gophercloud.PopulateApi(providerName)
	if err != nil {
		return nil, err
	}

	// if not given the default is used which is returned for that account.
	if o.Builder.RawRegion != "" {
		api.Region = o.Builder.RawRegion
	}

	csp, err := gophercloud.ServersApi(access, api)
	if err != nil {
		log.Printf("Region: %s", o.Builder.RawRegion)
		return nil, err
	}
	o.Client = csp

	return o, nil
}