Skip to content

Commit

Permalink
feat: Add user agent to CSO (PROJQUAY-7472) (#142)
Browse files Browse the repository at this point in the history
* feat: Add user agent to CSO (PROJQUAY-7472)
We add a user agent to CSO to better track CSO requests to Quay and/or other registries.
User agent reported version will now depend on `QUAY_VERSION` which is added as an env. variable to CSO deployment.

* Update go.mod
  • Loading branch information
ibazulic authored Aug 22, 2024
1 parent 10dfac7 commit 2bb8ac2
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=$BUILDPLATFORM golang:1.20 as builder
FROM --platform=$BUILDPLATFORM golang:1.20 AS builder

ARG TARGETOS TARGETARCH
WORKDIR /workspace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ metadata:
containerImage: quay.io/quay/container-security-operator@sha256:6eefeaee910251ba26c825746d11ae166a9781aeace5455b2766d26298911f13
createdAt: 2020-04-13 00:00:00
description: Identify image vulnerabilities in Kubernetes pods
quay-version: v3.11.0
repository: https://github.com/quay/container-security-operator
tectonic-visibility: ocs
features.operators.openshift.io/disconnected: "false"
Expand Down Expand Up @@ -76,6 +77,10 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.annotations['olm.targetNamespaces']
- name: QUAY_VERSION
valueFrom:
fieldRef:
fieldPath: metadata.annotations['quay-version']
volumeMounts:
- name: extra-certs
readOnly: true
Expand Down
3 changes: 1 addition & 2 deletions cmd/security-labeller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/tls"
"crypto/x509"
"flag"
"io/ioutil"
"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -125,7 +124,7 @@ func handleCerts(insecure bool, extraCerts string) error {
if info.IsDir() {
return nil
}
certs, err := ioutil.ReadFile(path)
certs, err := os.ReadFile(path)

if err != nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/go-kit/kit v0.12.0
github.com/go-kit/log v0.2.1
github.com/openshift/client-go v0.0.0-20230626133714-296133fbf75e
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.16.0
Expand All @@ -21,7 +22,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
Expand Down
4 changes: 2 additions & 2 deletions labeller/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package labeller

import (
"io/ioutil"
"io"
"os"
"time"

Expand Down Expand Up @@ -33,7 +33,7 @@ func LoadConfig(cfgPath string) (*Config, error) {
}
defer f.Close()

d, err := ioutil.ReadAll(f)
d, err := io.ReadAll(f)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion labeller/labeller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
"time"

log "github.com/go-kit/kit/log"
log "github.com/go-kit/log"
"github.com/stretchr/testify/assert"

secscanv1alpha1 "github.com/quay/container-security-operator/apis/secscan/v1alpha1"
Expand Down
15 changes: 13 additions & 2 deletions secscan/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package secscan
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -49,7 +50,7 @@ func (c *Client) GetLayerDataFromTemplate(template string, image *image.Image, f
return nil, fmt.Errorf("Request returned non-200 response: %s", resp.Status)
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand All @@ -68,6 +69,7 @@ func (c *Client) GetLayerDataFromTemplate(template string, image *image.Image, f
}

func layerDataFromTemplateRequest(template, method string, img *image.Image, params map[string]string) (*rest.Request, error) {
var userAgent string
replacer := strings.NewReplacer("{namespace}", img.Namespace, "{reponame}", img.Repository, "{digest}", img.Digest)
requestURI := replacer.Replace(template)
url, err := url.ParseRequestURI(requestURI)
Expand All @@ -80,6 +82,15 @@ func layerDataFromTemplateRequest(template, method string, img *image.Image, par
req = req.SetHeader("Authorization", fmt.Sprintf("Basic %s", img.Auth))
}

env, isPresent := os.LookupEnv("QUAY_VERSION")
if !isPresent {
userAgent = "container-security-operator/1.0.6"
} else {
userAgent = env
}

req = req.SetHeader("User-Agent", userAgent)

for key, val := range params {
req = req.SetParam(key, val)
}
Expand Down
8 changes: 4 additions & 4 deletions secscan/quay/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package quay
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -49,7 +49,7 @@ func (c *SecscanClient) ManifestSecurity(namespace, repository, digest string, v
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
// Error reading response body
return nil, err
Expand Down Expand Up @@ -79,7 +79,7 @@ func (c *SecscanClient) ManifestSecurityFromTemplate(template, namespace, reposi
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
// Error reading response body
return nil, err
Expand Down Expand Up @@ -113,7 +113,7 @@ func (c *SecscanClient) GetLayerData(image *image.Image, features, vulnerabiliti
return nil, fmt.Errorf("Request returned non-200 response: %s", resp.Status)
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
// Error reading response body
return nil, fmt.Errorf("failed to perform request: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions secscan/quay/wellknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package quay
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"

Expand Down Expand Up @@ -43,7 +43,7 @@ func (c *WellknownClient) AppCapabilities() (*AppCapabilities, error) {
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
// Error reading response body
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions secscan/wellknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package secscan
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -69,7 +69,7 @@ func NewWellknownClient(host, wellKnownEndpoint string) (*WellknownClient, error
return nil, fmt.Errorf("Request returned non-200 response: %s", resp.Status)
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2bb8ac2

Please sign in to comment.