Skip to content

Commit

Permalink
injector ValidateServiceAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
lghinet committed Nov 1, 2021
1 parent ae0e7e9 commit c3c9979
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 32 deletions.
14 changes: 9 additions & 5 deletions cmd/injector/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func main() {
//https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md
klog.InitFlags(nil)
kube.InitFlags(nil)
injector.BindConfigFlags(nil)

flag.Parse()
defer klog.Flush()
Expand All @@ -25,12 +26,15 @@ func main() {
ctx := context.Background()

kubeClient := kube.GetKubeClient()
uids, err := injector.AllowedControllersServiceAccountUID(ctx, kubeClient)
if err != nil {
log.Fatalf("failed to get authentication uids from services accounts: %s", err)
}
var authUIDs []string

err = injector.NewInjector(uids, cfg, kubeClient).Run(ctx)
if cfg.ValidateServiceAccount {
authUIDs, err = injector.AllowedControllersServiceAccountUID(ctx, kubeClient)
if err != nil {
log.Fatalf("failed to get authentication uids from services accounts: %s", err)
}
}
err = injector.NewInjector(authUIDs, cfg, kubeClient).Run(ctx)
if err != http.ErrServerClosed {
klog.Fatal(err)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/rusid/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func main() {
klog.InfoS("Rusid is using", "config", cfg)

//Start diagnostics server
go startDiagnosticsServer(mainCtx, wg, cfg.DiagnosticsPort, cfg.EnableMetrics,
go startDiagnosticsServer(mainCtx, wg, cfg.AppID, cfg.DiagnosticsPort, cfg.EnableMetrics,
// WithTimeout allows you to set a max overall timeout.
healthcheck.WithTimeout(5*time.Second),
healthcheck.WithChecker("component manager", compManager))
Expand All @@ -93,7 +93,7 @@ func shutdownOnInterrupt(cancel func()) {
}()
}

func startDiagnosticsServer(ctx context.Context, wg *sync.WaitGroup, port int,
func startDiagnosticsServer(ctx context.Context, wg *sync.WaitGroup, appId string, port int,
enableMetrics bool, options ...healthcheck.Option) {
wg.Add(1)
defer wg.Done()
Expand All @@ -102,7 +102,8 @@ func startDiagnosticsServer(ctx context.Context, wg *sync.WaitGroup, port int,
router.Handle("/healthz", healthcheck.HandlerFunc(options...))

if enableMetrics {
router.Handle("/metrics", metrics.GetPrometheusMetricHandler())
exporter := metrics.SetupPrometheusMetrics(appId)
router.HandleFunc("/metrics", exporter.ServeHTTP)
} else {
metrics.SetNoopMeterProvider()
}
Expand Down
9 changes: 2 additions & 7 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"os"

"sync"
"time"
)

type serviceMetrics struct {
pubsubMeter metric.Meter
hostname string
publishCount metric.Int64Counter
subscribeDuration metric.Int64Histogram
}
Expand All @@ -22,7 +21,7 @@ var (
s *serviceMetrics
)

func DefaultMonitoring() *serviceMetrics {
func DefaultPubSubMetrics() *serviceMetrics {
initOnce.Do(func() {
s = newServiceMetrics()
})
Expand All @@ -32,11 +31,9 @@ func DefaultMonitoring() *serviceMetrics {
func newServiceMetrics() *serviceMetrics {
meter := global.Meter("rusi.io/pubsub")
pubsubM := metric.Must(meter)
hostname, _ := os.Hostname()

return &serviceMetrics{
pubsubMeter: meter,
hostname: hostname,
publishCount: pubsubM.NewInt64Counter("pubsub.publish.count",
metric.WithDescription("The number of publishes")),
subscribeDuration: pubsubM.NewInt64Histogram("pubsub.subscribe.duration",
Expand All @@ -49,7 +46,6 @@ func (s *serviceMetrics) RecordPublishMessage(ctx context.Context, topic string,
s.pubsubMeter.RecordBatch(
ctx,
[]attribute.KeyValue{
attribute.String("hostname", s.hostname),
attribute.String("topic", topic),
attribute.Bool("success", success),
},
Expand All @@ -60,7 +56,6 @@ func (s *serviceMetrics) RecordSubscriberProcessingTime(ctx context.Context, top
s.pubsubMeter.RecordBatch(
ctx,
[]attribute.KeyValue{
attribute.String("hostname", s.hostname),
attribute.String("topic", topic),
attribute.Bool("success", success),
},
Expand Down
15 changes: 12 additions & 3 deletions internal/metrics/prometheus.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package metrics

import (
"context"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric/global"
sdk_metric "go.opentelemetry.io/otel/sdk/export/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
selector "go.opentelemetry.io/otel/sdk/metric/selector/simple"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"k8s.io/klog/v2"
"net/http"
"time"
)

func GetPrometheusMetricHandler() http.HandlerFunc {
func SetupPrometheusMetrics(appId string) *prometheus.Exporter {
config := prometheus.Config{}
r, _ := resource.New(context.Background(),
resource.WithHost(),
resource.WithAttributes(semconv.ServiceNameKey.String(appId)))

c := controller.New(
processor.NewFactory(
selector.NewWithHistogramDistribution(
Expand All @@ -22,12 +29,14 @@ func GetPrometheusMetricHandler() http.HandlerFunc {
sdk_metric.CumulativeExportKindSelector(),
processor.WithMemory(true),
),
controller.WithResource(r),
controller.WithCollectPeriod(10*time.Second), //default - 10 sec
)
exporter, err := prometheus.New(config, c)
if err != nil {
klog.ErrorS(err, "failed to initialize prometheus exporter")
return nil
}
global.SetMeterProvider(exporter.MeterProvider())
return exporter.ServeHTTP
return exporter
}
23 changes: 15 additions & 8 deletions pkg/injector/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package injector

import (
"flag"
"github.com/kelseyhightower/envconfig"
)

var defaultConfig = NewConfigWithDefaults()

// Config represents configuration options for the Rusi Sidecar Injector webhook server.
type Config struct {
TLSCertFile string `envconfig:"TLS_CERT_FILE" required:"true"`
Expand All @@ -12,6 +15,7 @@ type Config struct {
SidecarImagePullPolicy string `envconfig:"SIDECAR_IMAGE_PULL_POLICY"`
Namespace string `envconfig:"NAMESPACE" required:"true"`
KubeClusterDomain string `envconfig:"KUBE_CLUSTER_DOMAIN"`
ValidateServiceAccount bool
}

// NewConfigWithDefaults returns a Config object with default values already
Expand All @@ -20,17 +24,20 @@ type Config struct {
func NewConfigWithDefaults() Config {
return Config{
SidecarImagePullPolicy: "Always",
ValidateServiceAccount: true,
}
}

// GetConfig returns configuration derived from environment variables.
func GetConfig() (Config, error) {
// get config from environment variables
c := NewConfigWithDefaults()
err := envconfig.Process("", &c)
if err != nil {
return c, err
func BindConfigFlags(flagset *flag.FlagSet) {
if flagset == nil {
flagset = flag.CommandLine
}

return c, nil
flagset.BoolVar(&defaultConfig.ValidateServiceAccount, "validate_service_account", defaultConfig.ValidateServiceAccount, "If true, injector will validate that rusi sidecars can only be created by a specified list of serviceAccounts")
}

// GetConfig returns configuration derived from environment variables.
func GetConfig() (Config, error) {
err := envconfig.Process("", &defaultConfig)
return defaultConfig, err
}
2 changes: 1 addition & 1 deletion pkg/injector/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (i *injector) handleRequest(w http.ResponseWriter, r *http.Request) {
if err != nil {
klog.Errorf("Can't decode body: %v", err)
} else {
if !utils.StringSliceContains(ar.Request.UserInfo.UID, i.authUIDs) {
if i.config.ValidateServiceAccount && !utils.StringSliceContains(ar.Request.UserInfo.UID, i.authUIDs) {
err = errors.New(fmt.Sprintf("service account '%s' not on the list of allowed controller accounts", ar.Request.UserInfo.Username))
klog.Error(err)
} else if ar.Request.Kind.Kind != "Pod" {
Expand Down
4 changes: 2 additions & 2 deletions pkg/injector/pod_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const (
apiAddress = "rusi-api"
apiPort = 80
kubernetesMountPath = "/var/run/secrets/kubernetes.io/serviceaccount"
defaultConfig = "default"
defaultConfigName = "default"
defaultEnabledMetric = true
defaultMetricsPort = 9090
defaultSidecarDebug = false
Expand Down Expand Up @@ -219,7 +219,7 @@ func podContainsSidecarContainer(pod *corev1.Pod) bool {
}

func getConfig(annotations map[string]string) string {
return getStringAnnotationOrDefault(annotations, rusiConfigKey, defaultConfig)
return getStringAnnotationOrDefault(annotations, rusiConfigKey, defaultConfigName)
}

func getEnableDebug(annotations map[string]string) bool {
Expand Down
4 changes: 2 additions & 2 deletions pkg/middleware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func SubscriberMetricsMiddleware() messaging.Middleware {
return func(ctx context.Context, msg *messaging.MessageEnvelope) error {
start := time.Now()
err := next(ctx, msg)
metrics.DefaultMonitoring().RecordSubscriberProcessingTime(ctx, msg.Subject, err == nil, time.Since(start))
metrics.DefaultPubSubMetrics().RecordSubscriberProcessingTime(ctx, msg.Subject, err == nil, time.Since(start))
return err
}
}
Expand All @@ -22,7 +22,7 @@ func PublisherMetricsMiddleware() messaging.Middleware {
return func(next messaging.Handler) messaging.Handler {
return func(ctx context.Context, msg *messaging.MessageEnvelope) error {
err := next(ctx, msg)
metrics.DefaultMonitoring().RecordPublishMessage(ctx, msg.Subject, err == nil)
metrics.DefaultPubSubMetrics().RecordPublishMessage(ctx, msg.Subject, err == nil)
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
const (
defaultGRPCPort = 50003
defaultDiagnosticsPort = 8080
defaultEnableMetrics = false
defaultEnableMetrics = true
)

type ConfigBuilder struct {
Expand Down

0 comments on commit c3c9979

Please sign in to comment.