Skip to content

Use k8s secret file for bearer token to handle key rotations #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const (
caFile = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
collectionInterval = 60 * time.Second
// needs to start with "containerInsightsKubeAPIServerScraper" for histogram deltas in the emf exporter
jobName = "containerInsightsKubeAPIServerScraper"
jobName = "containerInsightsKubeAPIServerScraper"
serviceAccountTokenDefaultPath = "/var/run/secrets/kubernetes.io/serviceaccount/token" // #nosec
)

var controlPlaneMetricAllowList = []string{
Expand Down Expand Up @@ -72,7 +73,6 @@ type PrometheusScraperOpts struct {
Host component.Host
ClusterNameProvider clusterNameProvider
LeaderElection *LeaderElection
BearerToken string
}

func NewPrometheusScraper(opts PrometheusScraperOpts) (*PrometheusScraper, error) {
Expand Down Expand Up @@ -101,6 +101,10 @@ func NewPrometheusScraper(opts PrometheusScraperOpts) (*PrometheusScraper, error
CAFile: caFile,
InsecureSkipVerify: false,
},
Authorization: &configutil.Authorization{
Type: "Bearer",
CredentialsFile: serviceAccountTokenDefaultPath,
},
},
ScrapeInterval: model.Duration(collectionInterval),
ScrapeTimeout: model.Duration(collectionInterval),
Expand Down Expand Up @@ -145,12 +149,6 @@ func NewPrometheusScraper(opts PrometheusScraperOpts) (*PrometheusScraper, error
},
}

if opts.BearerToken != "" {
scrapeConfig.HTTPClientConfig.BearerToken = configutil.Secret(opts.BearerToken)
} else {
opts.TelemetrySettings.Logger.Warn("bearer token is not set, control plane metrics will not be published")
}

promConfig := prometheusreceiver.Config{
PrometheusConfig: &prometheusreceiver.PromConfig{
ScrapeConfigs: []*config.ScrapeConfig{scrapeConfig},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ func TestNewPrometheusScraperBadInputs(t *testing.T) {
Host: componenttest.NewNopHost(),
ClusterNameProvider: mockClusterNameProvider{},
LeaderElection: nil,
BearerToken: "",
},
{
Ctx: context.TODO(),
Expand All @@ -111,7 +110,6 @@ func TestNewPrometheusScraperBadInputs(t *testing.T) {
Host: componenttest.NewNopHost(),
ClusterNameProvider: mockClusterNameProvider{},
LeaderElection: &leaderElection,
BearerToken: "",
},
{
Ctx: context.TODO(),
Expand All @@ -121,7 +119,6 @@ func TestNewPrometheusScraperBadInputs(t *testing.T) {
Host: nil,
ClusterNameProvider: mockClusterNameProvider{},
LeaderElection: &leaderElection,
BearerToken: "",
},
{
Ctx: context.TODO(),
Expand All @@ -131,7 +128,6 @@ func TestNewPrometheusScraperBadInputs(t *testing.T) {
Host: componenttest.NewNopHost(),
ClusterNameProvider: nil,
LeaderElection: &leaderElection,
BearerToken: "",
},
}

Expand Down Expand Up @@ -172,7 +168,6 @@ func TestNewPrometheusScraperEndToEnd(t *testing.T) {
Host: componenttest.NewNopHost(),
ClusterNameProvider: mockClusterNameProvider{},
LeaderElection: &leaderElection,
BearerToken: "",
})
assert.NoError(t, err)
assert.Equal(t, mockClusterNameProvider{}, scraper.clusterNameProvider)
Expand Down
12 changes: 0 additions & 12 deletions receiver/awscontainerinsightreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
"go.uber.org/zap"
"k8s.io/client-go/rest"

ci "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/containerinsight"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/k8s/k8sclient"
Expand Down Expand Up @@ -270,16 +269,6 @@ func (acir *awsContainerInsightReceiver) initPrometheusScraper(ctx context.Conte
}

acir.settings.Logger.Debug("kube apiserver endpoint found", zap.String("endpoint", endpoint))
// use the same leader

restConfig, err := rest.InClusterConfig()
if err != nil {
return err
}
bearerToken := restConfig.BearerToken
if bearerToken == "" {
return errors.New("bearer token was empty")
}

acir.prometheusScraper, err = k8sapiserver.NewPrometheusScraper(k8sapiserver.PrometheusScraperOpts{
Ctx: ctx,
Expand All @@ -289,7 +278,6 @@ func (acir *awsContainerInsightReceiver) initPrometheusScraper(ctx context.Conte
Host: host,
ClusterNameProvider: hostInfo,
LeaderElection: leaderElection,
BearerToken: bearerToken,
})
return err
}
Expand Down
Loading