Skip to content

Commit

Permalink
Add entity namespaces to chronoctl
Browse files Browse the repository at this point in the history
This PR adds support for entity namespaces to chronoctl. It is
only exposed via an environment variable. It is not a CLI flag
because it is not meant to be used by a normal everyday user
since it requires internal Chronosphere credentials

sc-90822
  • Loading branch information
aschepis committed Apr 25, 2024
1 parent e18a46a commit b3337ae
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Added support for entity namespaces

## v1.5.0

### Added
Expand Down
10 changes: 10 additions & 0 deletions src/cmd/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const (
ChronosphereOrgKey = "CHRONOSPHERE_ORG" // fallback for CHRONOSPHERE_ORG_NAME
// ChronosphereAPITokenKey is the environment variable that specifies the Chronosphere API token
ChronosphereAPITokenKey = "CHRONOSPHERE_API_TOKEN"
// ChronosphereAPITokenKey is the environment variable that specifies the Chronosphere entity namespace

Check failure on line 51 in src/cmd/pkg/client/client.go

View workflow job for this annotation

GitHub Actions / build

exported: comment on exported const ChronosphereEntityNamespace should be of the form "ChronosphereEntityNamespace ..." (revive)

Check failure on line 51 in src/cmd/pkg/client/client.go

View workflow job for this annotation

GitHub Actions / build

exported: comment on exported const ChronosphereEntityNamespace should be of the form "ChronosphereEntityNamespace ..." (revive)
ChronosphereEntityNamespace = "CHRONOSPHERE_ENTITY_NAMESPACE"
)

// Clients is a list of clients our generated CLI needs access to.
Expand Down Expand Up @@ -129,6 +131,7 @@ func (f *Flags) Transport(component transport.Component, basePath string) (*http
InsecureSkipVerify: f.InsecureSkipVerify,
AllowHTTP: f.AllowHTTP,
DefaultBasePath: basePath,
EntityNamespace: f.getEntityNamespace(),
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -159,6 +162,13 @@ func (f *Flags) Timeout() time.Duration {
return time.Duration(f.TimeoutSeconds) * time.Second
}

func (f *Flags) getEntityNamespace() string {
if ns := os.Getenv(ChronosphereEntityNamespace); ns != "" {
return ns
}
return ""
}

func (f *Flags) getAPIToken() (string, error) {
if f.APIToken != "" && f.APITokenFilename != "" {
return "", errors.New("only one of --api-token and --api-token-filename can be set")
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func TestClientFlagsTransport(t *testing.T) {
assert.NotNil(t, tp.DefaultAuthentication)

if tt.wantInsecureSkipVerify {
httpTransport := tp.Transport.(swagger.RequestIDTrailerTransport).RT.(transport.VersionHeaderTransport).Rt.(*http.Transport) //nolint:errcheck
httpTransport := tp.Transport.(swagger.RequestIDTrailerTransport).RT.(transport.CustomHeaderTransport).Rt.(*http.Transport) //nolint:errcheck
assert.Equal(t, tt.wantInsecureSkipVerify, httpTransport.TLSClientConfig.InsecureSkipVerify)
}
})
Expand Down
30 changes: 20 additions & 10 deletions src/cmd/pkg/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type RuntimeConfig struct {
InsecureSkipVerify bool
AllowHTTP bool
DefaultBasePath string
EntityNamespace string
}

// New creates a new HTTP transport that can communicate with the Chronosphere API.
Expand Down Expand Up @@ -104,7 +105,10 @@ func New(config RuntimeConfig) (*httptransport.Runtime, error) {
}

transport.DefaultAuthentication = httptransport.APIKeyAuth(apiTokenHeader, "header", config.APIToken)
transport.Transport = xswagger.WithRequestIDTrailerTransport(withVersionHeader(config.Component, transport.Transport))

transport.Transport = xswagger.WithRequestIDTrailerTransport(
withCustomHeaders(config.Component, config.EntityNamespace, transport.Transport),
)
transport.Consumers[httpruntime.JSONMime] = xswagger.JSONConsumer()
transport.Consumers[httpruntime.HTMLMime] = xswagger.TextConsumer()
transport.Consumers[httpruntime.TextMime] = xswagger.TextConsumer()
Expand All @@ -115,18 +119,20 @@ func New(config RuntimeConfig) (*httptransport.Runtime, error) {

const userAgentHeader = "User-Agent"

// VersionHeaderTransport is a RoundTripper that adds a User-Agent header to all requests.
type VersionHeaderTransport struct {
agent string
Rt http.RoundTripper
// CustomHeaderTransport is a RoundTripper that adds a custom headres to all requests
// for example: User-Agent, and Chrono-Entity-Namespace
type CustomHeaderTransport struct {
agent string
entityNamespace string
Rt http.RoundTripper
}

func withVersionHeader(component Component, rt http.RoundTripper) http.RoundTripper {
func withCustomHeaders(component Component, entityNamespace string, rt http.RoundTripper) http.RoundTripper {
if rt == nil {
rt = http.DefaultTransport
}

return VersionHeaderTransport{
return CustomHeaderTransport{
Rt: rt,
agent: fmt.Sprintf("%s/%v-%v (%s; %s; %s)",
component,
Expand All @@ -136,11 +142,15 @@ func withVersionHeader(component Component, rt http.RoundTripper) http.RoundTrip
runtime.GOOS,
runtime.GOARCH,
),
entityNamespace: entityNamespace,
}
}

// RoundTrip implements the RoundTripper interface.
func (v VersionHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set(userAgentHeader, v.agent)
return v.Rt.RoundTrip(req)
func (c CustomHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set(userAgentHeader, c.agent)
if c.entityNamespace != "" {
req.Header.Set("Chrono-Entity-Namespace", c.entityNamespace)
}
return c.Rt.RoundTrip(req)
}

0 comments on commit b3337ae

Please sign in to comment.