fix auth model
This commit is contained in:
1 parent
f81285b151
commit
5e3800e9cb
10 files changed
+901
-70
No files matched your search
+100
-28
@@ -9,44 +9,109 @@ import (
|
||||
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/floatingips"
|
||||
)
|
||||
|
||||
// AuthMethod selects how the client obtains the token it uses for Neutron
|
||||
// calls.
|
||||
type AuthMethod string
|
||||
|
||||
const (
|
||||
// AuthMethodToken uses an admin-supplied token as-is (passthrough): the
|
||||
// token is validated against Keystone but never exchanged for a freshly
|
||||
// minted one, and the exact token value the operator provided is what
|
||||
// every subsequent Neutron call uses. This is the default — it matches
|
||||
// the deployment constraint that the admin credential is supplied
|
||||
// directly via the process environment. Its trade-off: there is no way
|
||||
// to renew the token automatically, since nothing longer-lived than the
|
||||
// token itself is available to re-authenticate with. When it expires,
|
||||
// the operator must reissue it and restart control-api.
|
||||
AuthMethodToken AuthMethod = "token"
|
||||
|
||||
// AuthMethodPassword has the client obtain its own token via ordinary
|
||||
// Keystone password authentication, and automatically re-authenticates
|
||||
// (mints a fresh token) whenever the current one is rejected — not
|
||||
// bounded by any single token's TTL, at the cost of holding a
|
||||
// long-lived password credential in the process environment instead of
|
||||
// a token.
|
||||
AuthMethodPassword AuthMethod = "password"
|
||||
)
|
||||
|
||||
// ClientConfig carries pre-resolved credential values (already read from
|
||||
// environment variables by the caller — see config.OpenStackAuth). Token
|
||||
// auth is required per the deployment constraint that the admin credential
|
||||
// is supplied via the process environment, not a config file.
|
||||
// environment variables by the caller — see config.OpenStackConfig). Some
|
||||
// form of admin credential is always required via the process environment,
|
||||
// never a config file; which fields are required depends on Method.
|
||||
type ClientConfig struct {
|
||||
AuthURL string
|
||||
Token string
|
||||
ProjectID string
|
||||
ProjectName string
|
||||
DomainName string
|
||||
Region string
|
||||
AuthURL string
|
||||
Method AuthMethod // "" is treated as AuthMethodToken
|
||||
|
||||
Token string // required when Method == AuthMethodToken
|
||||
|
||||
Username string // required when Method == AuthMethodPassword
|
||||
Password string
|
||||
UserDomainName string
|
||||
|
||||
ProjectID string
|
||||
Region string
|
||||
Interface string // "public" | "internal" | "admin"; "" defaults to "public"
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
networking *gophercloud.ServiceClient
|
||||
}
|
||||
|
||||
// NewClient authenticates against Keystone using a pre-issued admin token
|
||||
// and returns a Client scoped to the given project/region, backed by the
|
||||
// Neutron (networking v2) service catalog entry.
|
||||
func NewClient(ctx context.Context, cfg ClientConfig) (*Client, error) {
|
||||
if cfg.AuthURL == "" || cfg.Token == "" {
|
||||
return nil, fmt.Errorf("openstack: auth URL and token are required")
|
||||
// buildAuthOptions translates ClientConfig into gophercloud.AuthOptions. It
|
||||
// touches no network and returns only validation errors, so the auth-method
|
||||
// selection logic is unit-testable without a real OpenStack deployment.
|
||||
func buildAuthOptions(cfg ClientConfig) (gophercloud.AuthOptions, error) {
|
||||
if cfg.AuthURL == "" {
|
||||
return gophercloud.AuthOptions{}, fmt.Errorf("openstack: auth URL is required")
|
||||
}
|
||||
if cfg.ProjectID == "" {
|
||||
return gophercloud.AuthOptions{}, fmt.Errorf("openstack: project ID is required")
|
||||
}
|
||||
|
||||
authOpts := gophercloud.AuthOptions{
|
||||
IdentityEndpoint: cfg.AuthURL,
|
||||
TokenID: cfg.Token,
|
||||
TenantID: cfg.ProjectID,
|
||||
TenantName: cfg.ProjectName,
|
||||
DomainName: cfg.DomainName,
|
||||
}
|
||||
if cfg.ProjectID != "" || cfg.ProjectName != "" {
|
||||
authOpts.Scope = &gophercloud.AuthScope{
|
||||
ProjectID: cfg.ProjectID,
|
||||
ProjectName: cfg.ProjectName,
|
||||
DomainName: cfg.DomainName,
|
||||
opts := gophercloud.AuthOptions{IdentityEndpoint: cfg.AuthURL}
|
||||
|
||||
switch cfg.Method {
|
||||
case AuthMethodPassword:
|
||||
if cfg.Username == "" || cfg.Password == "" {
|
||||
return gophercloud.AuthOptions{}, fmt.Errorf("openstack: username and password are required for auth_method=password")
|
||||
}
|
||||
opts.Username = cfg.Username
|
||||
opts.Password = cfg.Password
|
||||
opts.DomainName = cfg.UserDomainName
|
||||
opts.Scope = &gophercloud.AuthScope{ProjectID: cfg.ProjectID}
|
||||
// Safe and valuable here: a password credential can always mint a
|
||||
// fresh token, so gophercloud can transparently re-authenticate on
|
||||
// 401 for the entire lifetime of the process.
|
||||
opts.AllowReauth = true
|
||||
|
||||
case AuthMethodToken, "":
|
||||
if cfg.Token == "" {
|
||||
return gophercloud.AuthOptions{}, fmt.Errorf("openstack: token is required for auth_method=token")
|
||||
}
|
||||
opts.TokenID = cfg.Token
|
||||
// Scope is deliberately left unset: gophercloud's v3auth takes this
|
||||
// as the signal to "pass through" the given token rather than
|
||||
// exchange it for a new one (GET /v3/auth/tokens to validate +
|
||||
// fetch the catalog, using the same token value for every
|
||||
// subsequent call, never minting a replacement). AllowReauth is
|
||||
// left false on purpose — gophercloud actively rejects AllowReauth
|
||||
// when Scope is unset, and there's nothing to reauthenticate with
|
||||
// beyond the one token we were given anyway.
|
||||
|
||||
default:
|
||||
return gophercloud.AuthOptions{}, fmt.Errorf("openstack: unknown auth method %q", cfg.Method)
|
||||
}
|
||||
|
||||
return opts, nil
|
||||
}
|
||||
|
||||
// NewClient authenticates against Keystone (via the method selected by
|
||||
// cfg.Method) and returns a Client scoped to the given project/region,
|
||||
// backed by the Neutron (networking v2) service catalog entry.
|
||||
func NewClient(ctx context.Context, cfg ClientConfig) (*Client, error) {
|
||||
authOpts, err := buildAuthOptions(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
provider, err := osauth.AuthenticatedClient(ctx, authOpts)
|
||||
@@ -54,7 +119,14 @@ func NewClient(ctx context.Context, cfg ClientConfig) (*Client, error) {
|
||||
return nil, fmt.Errorf("openstack: authenticate: %w", err)
|
||||
}
|
||||
|
||||
networking, err := osauth.NewNetworkV2(provider, gophercloud.EndpointOpts{Region: cfg.Region})
|
||||
iface := cfg.Interface
|
||||
if iface == "" {
|
||||
iface = string(gophercloud.AvailabilityPublic)
|
||||
}
|
||||
networking, err := osauth.NewNetworkV2(provider, gophercloud.EndpointOpts{
|
||||
Region: cfg.Region,
|
||||
Availability: gophercloud.Availability(iface),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("openstack: networking client: %w", err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user