2020-10-05 13:45:33 +00:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2020-10-16 18:20:57 +00:00
|
|
|
"errors"
|
2020-10-05 13:45:33 +00:00
|
|
|
"fmt"
|
2022-08-21 21:13:44 +00:00
|
|
|
"io"
|
2020-10-05 13:45:33 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
2020-10-16 18:20:57 +00:00
|
|
|
"strings"
|
2020-10-05 13:45:33 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
var configMap = discoveryutils.NewConfigMap()
|
|
|
|
|
|
|
|
// apiCredentials can be refreshed
|
|
|
|
type apiCredentials struct {
|
|
|
|
computeURL *url.URL
|
|
|
|
token string
|
|
|
|
expiration time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type apiConfig struct {
|
|
|
|
client *http.Client
|
|
|
|
port int
|
2020-10-05 15:11:51 +00:00
|
|
|
// tokenLock guards creds refresh
|
2020-10-05 13:45:33 +00:00
|
|
|
tokenLock sync.Mutex
|
|
|
|
creds *apiCredentials
|
2022-02-14 11:07:30 +00:00
|
|
|
// authTokenReq contains request body for apiCredentials
|
2020-10-05 13:45:33 +00:00
|
|
|
authTokenReq []byte
|
|
|
|
// keystone endpoint
|
|
|
|
endpoint *url.URL
|
|
|
|
allTenants bool
|
|
|
|
region string
|
|
|
|
// availability public, internal, admin for filtering compute endpoint
|
|
|
|
availability string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *apiConfig) getFreshAPICredentials() (*apiCredentials, error) {
|
|
|
|
cfg.tokenLock.Lock()
|
|
|
|
defer cfg.tokenLock.Unlock()
|
|
|
|
|
2020-10-05 15:11:51 +00:00
|
|
|
if cfg.creds != nil && time.Until(cfg.creds.expiration) > 10*time.Second {
|
2020-10-05 13:45:33 +00:00
|
|
|
// Credentials aren't expired yet.
|
|
|
|
return cfg.creds, nil
|
|
|
|
}
|
|
|
|
newCreds, err := getCreds(cfg)
|
|
|
|
if err != nil {
|
2020-10-05 15:11:51 +00:00
|
|
|
return nil, fmt.Errorf("cannot refresh OpenStack api token: %w", err)
|
2020-10-05 13:45:33 +00:00
|
|
|
}
|
|
|
|
cfg.creds = newCreds
|
2020-10-06 08:20:36 +00:00
|
|
|
logger.Infof("successfully refreshed OpenStack api token; expiration: %.3f seconds", time.Until(newCreds.expiration).Seconds())
|
2020-10-05 15:11:51 +00:00
|
|
|
return newCreds, nil
|
2020-10-05 13:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getAPIConfig(sdc *SDConfig, baseDir string) (*apiConfig, error) {
|
|
|
|
v, err := configMap.Get(sdc, func() (interface{}, error) { return newAPIConfig(sdc, baseDir) })
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return v.(*apiConfig), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAPIConfig(sdc *SDConfig, baseDir string) (*apiConfig, error) {
|
2023-02-11 22:41:00 +00:00
|
|
|
port := sdc.Port
|
|
|
|
if port == 0 {
|
|
|
|
port = 80
|
|
|
|
}
|
2020-10-05 13:45:33 +00:00
|
|
|
cfg := &apiConfig{
|
2021-05-14 15:10:19 +00:00
|
|
|
client: &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
MaxIdleConnsPerHost: 100,
|
|
|
|
},
|
|
|
|
},
|
2020-10-05 13:45:33 +00:00
|
|
|
availability: sdc.Availability,
|
|
|
|
region: sdc.Region,
|
|
|
|
allTenants: sdc.AllTenants,
|
2023-02-11 22:41:00 +00:00
|
|
|
port: port,
|
2020-10-05 13:45:33 +00:00
|
|
|
}
|
|
|
|
if sdc.TLSConfig != nil {
|
2022-07-04 11:27:48 +00:00
|
|
|
opts := &promauth.Options{
|
|
|
|
BaseDir: baseDir,
|
|
|
|
TLSConfig: sdc.TLSConfig,
|
|
|
|
}
|
|
|
|
ac, err := opts.NewConfig()
|
2020-10-05 13:45:33 +00:00
|
|
|
if err != nil {
|
2023-07-27 21:47:53 +00:00
|
|
|
cfg.client.CloseIdleConnections()
|
2023-10-25 21:19:33 +00:00
|
|
|
return nil, fmt.Errorf("cannot parse TLS config: %w", err)
|
|
|
|
}
|
2024-04-03 21:46:40 +00:00
|
|
|
cfg.client.Transport = ac.NewRoundTripper(&http.Transport{
|
|
|
|
MaxIdleConnsPerHost: 100,
|
2024-04-03 08:01:43 +00:00
|
|
|
})
|
2020-10-05 13:45:33 +00:00
|
|
|
}
|
|
|
|
// use public compute endpoint by default
|
|
|
|
if len(cfg.availability) == 0 {
|
|
|
|
cfg.availability = "public"
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new variable to prevent side effects
|
|
|
|
sdcAuth := *sdc
|
|
|
|
// special case if identity_endpoint is not defined
|
2020-10-05 15:11:51 +00:00
|
|
|
if len(sdcAuth.IdentityEndpoint) == 0 {
|
2020-10-05 13:45:33 +00:00
|
|
|
// override sdc
|
|
|
|
sdcAuth = readCredentialsFromEnv()
|
|
|
|
}
|
2020-10-16 18:20:57 +00:00
|
|
|
if strings.HasSuffix(sdcAuth.IdentityEndpoint, "v2.0") {
|
2023-07-27 21:47:53 +00:00
|
|
|
cfg.client.CloseIdleConnections()
|
2020-10-16 18:20:57 +00:00
|
|
|
return nil, errors.New("identity_endpoint v2.0 is not supported")
|
|
|
|
}
|
|
|
|
// trim .0 from v3.0 for prometheus cfg compatibility
|
|
|
|
sdcAuth.IdentityEndpoint = strings.TrimSuffix(sdcAuth.IdentityEndpoint, ".0")
|
2020-10-05 13:45:33 +00:00
|
|
|
|
|
|
|
parsedURL, err := url.Parse(sdcAuth.IdentityEndpoint)
|
|
|
|
if err != nil {
|
2023-07-27 21:47:53 +00:00
|
|
|
cfg.client.CloseIdleConnections()
|
2023-10-25 21:19:33 +00:00
|
|
|
return nil, fmt.Errorf("cannot parse identity_endpoint %s as url: %w", sdcAuth.IdentityEndpoint, err)
|
2020-10-05 13:45:33 +00:00
|
|
|
}
|
|
|
|
cfg.endpoint = parsedURL
|
|
|
|
tokenReq, err := buildAuthRequestBody(&sdcAuth)
|
|
|
|
if err != nil {
|
2023-07-27 21:47:53 +00:00
|
|
|
cfg.client.CloseIdleConnections()
|
2020-10-05 13:45:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cfg.authTokenReq = tokenReq
|
2020-10-05 15:11:51 +00:00
|
|
|
// cfg.creds is populated at getFreshAPICredentials
|
2020-10-05 13:45:33 +00:00
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
2020-10-05 15:11:51 +00:00
|
|
|
// getCreds makes a call to openstack keystone api and retrieves token and computeURL
|
|
|
|
//
|
|
|
|
// See https://docs.openstack.org/api-ref/identity/v3/
|
2020-10-05 13:45:33 +00:00
|
|
|
func getCreds(cfg *apiConfig) (*apiCredentials, error) {
|
|
|
|
apiURL := *cfg.endpoint
|
|
|
|
apiURL.Path = path.Join(apiURL.Path, "auth", "tokens")
|
|
|
|
|
|
|
|
resp, err := cfg.client.Post(apiURL.String(), "application/json", bytes.NewBuffer(cfg.authTokenReq))
|
|
|
|
if err != nil {
|
2023-10-25 21:19:33 +00:00
|
|
|
return nil, fmt.Errorf("failed query openstack identity api at url %s: %w", apiURL.String(), err)
|
2020-10-05 13:45:33 +00:00
|
|
|
}
|
2022-08-21 21:13:44 +00:00
|
|
|
r, err := io.ReadAll(resp.Body)
|
2020-10-05 13:45:33 +00:00
|
|
|
_ = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot read response from %q: %w", apiURL.String(), err)
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusCreated {
|
|
|
|
return nil, fmt.Errorf("auth failed, bad status code: %d, want: 201", resp.StatusCode)
|
|
|
|
}
|
|
|
|
at := resp.Header.Get("X-Subject-Token")
|
|
|
|
if len(at) == 0 {
|
|
|
|
return nil, fmt.Errorf("auth failed, response without X-Subject-Token")
|
|
|
|
}
|
|
|
|
var ar authResponse
|
|
|
|
if err := json.Unmarshal(r, &ar); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse auth credentials response: %w", err)
|
|
|
|
}
|
|
|
|
computeURL, err := getComputeEndpointURL(ar.Token.Catalog, cfg.availability, cfg.region)
|
|
|
|
if err != nil {
|
2020-10-05 15:11:51 +00:00
|
|
|
return nil, fmt.Errorf("cannot get computeEndpoint, account doesn't have enough permissions, "+
|
|
|
|
"availability: %s, region: %s; error: %w", cfg.availability, cfg.region, err)
|
2020-10-05 13:45:33 +00:00
|
|
|
}
|
|
|
|
return &apiCredentials{
|
|
|
|
token: at,
|
|
|
|
expiration: ar.Token.ExpiresAt,
|
|
|
|
computeURL: computeURL,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-05 15:11:51 +00:00
|
|
|
// readResponseBody reads body from http.Response.
|
2020-10-05 13:45:33 +00:00
|
|
|
func readResponseBody(resp *http.Response, apiURL string) ([]byte, error) {
|
2022-08-21 21:13:44 +00:00
|
|
|
data, err := io.ReadAll(resp.Body)
|
2020-10-05 13:45:33 +00:00
|
|
|
_ = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot read response from %q: %w", apiURL, err)
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("unexpected status code for %q; got %d; want %d; response body: %q",
|
|
|
|
apiURL, resp.StatusCode, http.StatusOK, data)
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2020-10-05 15:11:51 +00:00
|
|
|
// getAPIResponse calls openstack apiURL and returns response body.
|
|
|
|
func getAPIResponse(apiURL string, cfg *apiConfig) ([]byte, error) {
|
|
|
|
creds, err := cfg.getFreshAPICredentials()
|
2020-10-05 13:45:33 +00:00
|
|
|
if err != nil {
|
2020-10-05 15:11:51 +00:00
|
|
|
return nil, err
|
2020-10-05 13:45:33 +00:00
|
|
|
}
|
2023-02-23 02:53:05 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, apiURL, nil)
|
2020-10-05 13:45:33 +00:00
|
|
|
if err != nil {
|
2020-10-05 15:11:51 +00:00
|
|
|
return nil, fmt.Errorf("cannot create new request for openstack api url %s: %w", apiURL, err)
|
2020-10-05 13:45:33 +00:00
|
|
|
}
|
2020-10-05 15:11:51 +00:00
|
|
|
req.Header.Set("X-Auth-Token", creds.token)
|
2020-10-05 13:45:33 +00:00
|
|
|
resp, err := cfg.client.Do(req)
|
|
|
|
if err != nil {
|
2020-10-05 15:11:51 +00:00
|
|
|
return nil, fmt.Errorf("cannot query openstack api url %s: %w", apiURL, err)
|
2020-10-05 13:45:33 +00:00
|
|
|
}
|
2020-10-05 15:11:51 +00:00
|
|
|
return readResponseBody(resp, apiURL)
|
2020-10-05 13:45:33 +00:00
|
|
|
|
|
|
|
}
|