Skip to content

Commit

Permalink
fix(insights): set User-Agent header for UHC Auth Proxy (#677)
Browse files Browse the repository at this point in the history
* fix(insights): set User-Agent header for UHC Auth Proxy

* Separate operator version from image version
  • Loading branch information
ebaron authored Nov 14, 2023
1 parent 2fc1e3e commit a05b517
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ OS = $(shell go env GOOS)
ARCH = $(shell go env GOARCH)

# Current Operator version
IMAGE_VERSION ?= 2.5.0-dev
export OPERATOR_VERSION ?= 2.5.0-dev
IMAGE_VERSION ?= $(OPERATOR_VERSION)
BUNDLE_VERSION ?= $(IMAGE_VERSION)
DEFAULT_NAMESPACE ?= quay.io/cryostat
IMAGE_NAMESPACE ?= $(DEFAULT_NAMESPACE)
Expand Down Expand Up @@ -128,7 +129,7 @@ INSIGHTS_PROXY_NAMESPACE ?= quay.io/3scale
INSIGHTS_PROXY_NAME ?= apicast
INSIGHTS_PROXY_VERSION ?= insights-01
export INSIGHTS_PROXY_IMG ?= $(INSIGHTS_PROXY_NAMESPACE)/$(INSIGHTS_PROXY_NAME):$(INSIGHTS_PROXY_VERSION)
export INSIGHTS_BACKEND ?= cert.console.redhat.com
export INSIGHTS_BACKEND ?= console.redhat.com
else
KUSTOMIZE_DIR ?= config/default
endif
Expand Down
10 changes: 9 additions & 1 deletion bundle/manifests/cryostat-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ metadata:
capabilities: Seamless Upgrades
categories: Monitoring, Developer Tools
containerImage: quay.io/cryostat/cryostat-operator:2.5.0-dev
createdAt: "2023-11-07T20:18:21Z"
createdAt: "2023-11-13T21:47:45Z"
description: JVM monitoring and profiling tool
operatorframework.io/initialization-resource: |-
{
Expand Down Expand Up @@ -971,6 +971,14 @@ spec:
- list
- update
- watch
- apiGroups:
- config.openshift.io
resources:
- clusterversions
verbs:
- get
- list
- watch
- apiGroups:
- console.openshift.io
resources:
Expand Down
2 changes: 1 addition & 1 deletion config/insights/insights_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ spec:
- name: INSIGHTS_ENABLED
value: "true"
- name: INSIGHTS_BACKEND_DOMAIN
value: "cert.console.redhat.com"
value: "console.redhat.com"
8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ rules:
- list
- update
- watch
- apiGroups:
- config.openshift.io
resources:
- clusterversions
verbs:
- get
- list
- watch
- apiGroups:
- console.openshift.io
resources:
Expand Down
3 changes: 3 additions & 0 deletions internal/controllers/const_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/controllers/insights/apicast.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type apiCastConfigParams struct {
FrontendDomains string
BackendInsightsDomain string
HeaderValue string
UserAgent string
ProxyDomain string
}

Expand Down Expand Up @@ -63,6 +64,12 @@ var apiCastConfigTemplate = template.Must(template.New("").Parse(`{
"header": "Authorization",
"value_type": "plain",
"value": "Bearer {{ .HeaderValue }}"
},
{
"op": "set",
"header": "User-Agent",
"value_type": "plain",
"value": "{{ .UserAgent }}"
}
]
}
Expand Down
19 changes: 19 additions & 0 deletions internal/controllers/insights/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
"fmt"
"strings"

"github.com/cryostatio/cryostat-operator/internal/controllers"
"github.com/cryostatio/cryostat-operator/internal/controllers/common"
"github.com/cryostatio/cryostat-operator/internal/controllers/constants"
configv1 "github.com/openshift/api/config/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -63,11 +65,17 @@ func (r *InsightsReconciler) reconcilePullSecret(ctx context.Context) error {
return err
}

userAgent, err := r.getUserAgentString(ctx)
if err != nil {
return err
}

params := &apiCastConfigParams{
FrontendDomains: fmt.Sprintf("\"%s\",\"%s.%s.svc.cluster.local\"", ProxyServiceName, ProxyServiceName, r.Namespace),
BackendInsightsDomain: r.backendDomain,
ProxyDomain: r.proxyDomain,
HeaderValue: *token,
UserAgent: *userAgent,
}
config, err := getAPICastConfig(params)
if err != nil {
Expand Down Expand Up @@ -149,6 +157,17 @@ func (r *InsightsReconciler) getTokenFromPullSecret(ctx context.Context) (*strin
return &token, nil
}

func (r *InsightsReconciler) getUserAgentString(ctx context.Context) (*string, error) {
cv := &configv1.ClusterVersion{}
err := r.Client.Get(ctx, types.NamespacedName{Name: "version"}, cv)
if err != nil {
return nil, err
}

userAgent := fmt.Sprintf("cryostat-operator/%s cluster/%s", controllers.OperatorVersion, cv.Spec.ClusterID)
return &userAgent, nil
}

func (r *InsightsReconciler) createOrUpdateProxySecret(ctx context.Context, secret *corev1.Secret, owner metav1.Object,
config string) error {
op, err := controllerutil.CreateOrUpdate(ctx, r.Client, secret, func() error {
Expand Down
1 change: 1 addition & 0 deletions internal/controllers/insights/insights_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func NewInsightsReconciler(config *InsightsReconcilerConfig) (*InsightsReconcile

// +kubebuilder:rbac:groups=apps,resources=deployments;deployments/finalizers,verbs=*
// +kubebuilder:rbac:groups="",resources=services;secrets;configmaps;configmaps/finalizers,verbs=*
// +kubebuilder:rbac:groups=config.openshift.io,resources=clusterversions,verbs=get;list;watch

// Reconcile processes the Insights proxy deployment and configures it accordingly
func (r *InsightsReconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/controllers/insights/insights_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var _ = Describe("InsightsController", func() {
t.objs = []ctrlclient.Object{
t.NewNamespace(),
t.NewGlobalPullSecret(),
t.NewClusterVersion(),
t.NewOperatorDeployment(),
t.NewProxyConfigMap(),
}
Expand Down
30 changes: 28 additions & 2 deletions internal/controllers/insights/test/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"

"github.com/cryostatio/cryostat-operator/internal/test"
configv1 "github.com/openshift/api/config/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand All @@ -30,6 +31,8 @@ type InsightsTestResources struct {
Resources *corev1.ResourceRequirements
}

const expectedOperatorVersion = "2.5.0-dev"

func (r *InsightsTestResources) NewGlobalPullSecret() *corev1.Secret {
config := `{"auths":{"example.com":{"auth":"hello"},"cloud.openshift.com":{"auth":"world"}}}`
return &corev1.Secret{
Expand Down Expand Up @@ -118,6 +121,12 @@ func (r *InsightsTestResources) NewInsightsProxySecret() *corev1.Secret {
"header": "Authorization",
"value_type": "plain",
"value": "Bearer world"
},
{
"op": "set",
"header": "User-Agent",
"value_type": "plain",
"value": "cryostat-operator/%s cluster/abcde"
}
]
}
Expand All @@ -139,7 +148,7 @@ func (r *InsightsTestResources) NewInsightsProxySecret() *corev1.Secret {
}
}
]
}`, r.Namespace),
}`, r.Namespace, expectedOperatorVersion),
},
}
}
Expand Down Expand Up @@ -186,6 +195,12 @@ func (r *InsightsTestResources) NewInsightsProxySecretWithProxyDomain() *corev1.
"header": "Authorization",
"value_type": "plain",
"value": "Bearer world"
},
{
"op": "set",
"header": "User-Agent",
"value_type": "plain",
"value": "cryostat-operator/%s cluster/abcde"
}
]
}
Expand All @@ -207,7 +222,7 @@ func (r *InsightsTestResources) NewInsightsProxySecretWithProxyDomain() *corev1.
}
}
]
}`, r.Namespace),
}`, r.Namespace, expectedOperatorVersion),
},
}
}
Expand Down Expand Up @@ -365,3 +380,14 @@ func (r *InsightsTestResources) NewInsightsProxyService() *corev1.Service {
},
}
}

func (r *InsightsTestResources) NewClusterVersion() *configv1.ClusterVersion {
return &configv1.ClusterVersion{
ObjectMeta: metav1.ObjectMeta{
Name: "version",
},
Spec: configv1.ClusterVersionSpec{
ClusterID: "abcde",
},
}
}
11 changes: 7 additions & 4 deletions internal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,12 @@ func main() {
}

// Optionally enable Insights integration. Will only be enabled if INSIGHTS_ENABLED is true
insightsURL, err := insights.NewInsightsIntegration(mgr, &setupLog).Setup()
if err != nil {
setupLog.Error(err, "failed to set up Insights integration")
var insightsURL *url.URL
if openShift {
insightsURL, err = insights.NewInsightsIntegration(mgr, &setupLog).Setup()
if err != nil {
setupLog.Error(err, "failed to set up Insights integration")
}
}

config := newReconcilerConfig(mgr, "ClusterCryostat", "clustercryostat-controller", openShift,
Expand Down Expand Up @@ -185,7 +188,7 @@ func main() {
os.Exit(1)
}

setupLog.Info("starting manager")
setupLog.Info("starting manager", "version", controllers.OperatorVersion)
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
Expand Down
6 changes: 6 additions & 0 deletions internal/tools/const_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

const appNameEnv = "APP_NAME"
const operatorVersionEnv = "OPERATOR_VERSION"
const coreImageEnv = "CORE_IMG"
const datasourceImageEnv = "DATASOURCE_IMG"
const grafanaImageEnv = "GRAFANA_IMG"
Expand All @@ -35,12 +36,14 @@ func main() {
// Fill in image tags struct from the environment variables
consts := struct {
AppName string
OperatorVersion string
CoreImageTag string
DatasourceImageTag string
GrafanaImageTag string
ReportsImageTag string
}{
AppName: getEnvVar(appNameEnv),
OperatorVersion: getEnvVar(operatorVersionEnv),
CoreImageTag: getEnvVar(coreImageEnv),
DatasourceImageTag: getEnvVar(datasourceImageEnv),
GrafanaImageTag: getEnvVar(grafanaImageEnv),
Expand Down Expand Up @@ -74,6 +77,9 @@ package controllers
// User facing name of the operand application
const AppName = "{{ .AppName }}"
// Version of the Cryostat Operator
const OperatorVersion = "{{ .OperatorVersion }}"
// Default image tag for the core application image
const DefaultCoreImageTag = "{{ .CoreImageTag }}"
Expand Down

0 comments on commit a05b517

Please sign in to comment.