Skip to content
Draft
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
4 changes: 4 additions & 0 deletions .configs/gqlgen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ autobind:
- "github.com/nais/api/internal/usersync"
- "github.com/nais/api/internal/utilization"
- "github.com/nais/api/internal/vulnerability"
- "github.com/nais/api/internal/v0s"
- "github.com/nais/api/internal/workload"
- "github.com/nais/api/internal/workload/application"
- "github.com/nais/api/internal/workload/job"
Expand Down Expand Up @@ -144,3 +145,6 @@ models:
PageInfo:
model:
- github.com/nais/api/internal/graph/pagination.PageInfo
fields:
totalCount:
fieldName: Count
10 changes: 10 additions & 0 deletions .mise-tasks/generate/proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ protoc \
--go_out=. \
--go-grpc_out=.

protoc \
-I internal/v0s/protoapi/schema/ \
internal/v0s/protoapi/schema/v0s.proto \
--go_opt='Mv0s.proto=github.com/nais/api/internal/v0s/protoapi;protoapi' \
--go_opt=module=github.com/nais/api \
--go_out=. \
--go-grpc_opt='Mv0s.proto=github.com/nais/api/internal/v0s/protoapi;protoapi' \
--go-grpc_opt=module=github.com/nais/api \
--go-grpc_out=.

mise run fmt:go
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,48 @@ Existing clients can keep using configured aliases. The additive
allowing newer clients to warn about legacy aliases without duplicating this
configuration. Activity log entries use the canonical name.

## v0s migration

The new `vulnerabilityFindings`, `vulnerabilityFindingSummary`,
`vulnerabilityCoverage`, `vulnerability`, and `vulnerabilitySuppressions` queries
read v0s. Applications and jobs also expose findings, summary and coverage.
Existing v13s queries and mutations keep their existing source and behavior.

Set `V0S_ENDPOINT` to the plaintext gRPC endpoint allowed by v0s's NetworkPolicy.
The connection is lazy: an unset endpoint or unavailable v0s does not block API
startup, workload inventory, or legacy vulnerability fields. New fields return
errors rather than silently falling back to v13s or reporting zero findings.
Environment names are the same user-facing names used by `Workloads.ListImages`.

`V0S_SUPPRESSIONS_ENABLED` defaults to false. Keep it false while v13s owns
assessments and synchronizes them into v0s. Enable only after old user writes are
frozen, synchronization has caught up, and the sync worker and its retries have
stopped. The new mutations authorize the whole team's batch and derive the actor
from the authenticated user. Deletions do not require an active finding.
Successful sets and deletions are recorded in the team's activity log.

New lists are forward-only (`first` from 1 to 200) and use the standard
`edges`, `nodes` and `PageInfo` contract. `totalCount` covers the full filtered
selection and is loaded only when requested; counting suppressions currently
pages through the matching assessments. `pageStart` and `pageEnd` track positions
in the traversal; concurrent updates can change the total. Root finding and suppression queries
take scope inside `filter.scope`; workload fields are already scoped.
Findings, advisories and assessments have global IDs and can be fetched through
`node`. A finding's ID includes its image digest, so an obsolete-image finding
is not returned after the workload changes images.

Opaque cursors wrap v0s's per-item tokens and are scoped to the operation and
filter. They remain usable when an earlier item disappears, and cannot be used
with the old offset-paginated fields. Deploy v0s with per-item cursor support
before enabling the new API reads. Coverage uses current API inventory;
workloads absent from v0s or still associated with an old image are pending, not
clean. Removed workloads and old-image findings are excluded.

The pinned v0s protobuf schema is in `internal/v0s/protoapi/schema/v0s.proto`.
`mise run generate:proto` generates the client locally, without depending on the
v0s scanner module or its Go version. Update the pinned schema and regenerate when
the upstream contract changes.

## Development practices

Check practices used in this project here: [docs/practices.md](docs/practices.md)
Expand Down
12 changes: 12 additions & 0 deletions internal/cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/nais/api/internal/thirdparty/hookd"
fakehookd "github.com/nais/api/internal/thirdparty/hookd/fake"
"github.com/nais/api/internal/unleash"
"github.com/nais/api/internal/v0s"
"github.com/nais/api/internal/vulnerability"
"github.com/sethvargo/go-envconfig"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -222,6 +223,16 @@ func run(ctx context.Context, cfg *Config, log logrus.FieldLogger) error {
}

var mgmtK8sClient k8s.Interface
v0sMgr, err := v0s.NewManager(cfg.V0s.Endpoint, cfg.V0s.SuppressionsEnabled)
if err != nil {
return err
}
defer func() {
if err := v0sMgr.Close(); err != nil {
log.WithError(err).Warn("close v0s client")
}
}()

if cfg.Fakes.WithFakeKubernetes || cfg.Fakes.WithFakeManagementKubernetes {
mgmtK8sClient = k8sfake.NewSimpleClientset()
} else {
Expand Down Expand Up @@ -339,6 +350,7 @@ func run(ctx context.Context, cfg *Config, log logrus.FieldLogger) error {
cfg.Aiven.Projects,
cfg.KafkaEnvironments,
vulnMgr,
v0sMgr,
cfg.Tenant,
cfg.K8s.AllClusterNames(),
hookdClient,
Expand Down
7 changes: 7 additions & 0 deletions internal/cmd/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ type Config struct {
Usersync usersyncConfig
Cost costConfig
VulnerabilitiesAPI vulnerabilitiesConfig
V0s v0sConfig
Hookd hookdConfig
OAuth oAuthConfig
Unleash UnleashConfig
Expand All @@ -206,6 +207,12 @@ type Config struct {
AuditLog auditLogConfig
}

type v0sConfig struct {
Endpoint string `env:"V0S_ENDPOINT"`
// Enable only after v13s user writes and suppression synchronization are stopped.
SuppressionsEnabled bool `env:"V0S_SUPPRESSIONS_ENABLED"`
}

type Fakes struct {
WithInsecureWebhookURLs bool `env:"WITH_INSECURE_WEBHOOK_URLS"`
WithInsecureUserHeader bool `env:"WITH_INSECURE_USER_HEADER"`
Expand Down
13 changes: 13 additions & 0 deletions internal/cmd/api/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestKafkaEnvironmentsConfig(t *testing.T) {
if (err != nil) != tt.wantErr {
t.Fatalf("NewConfig() error = %v, wantErr %v", err, tt.wantErr)
}

if tt.wantErr {
return
}
Expand All @@ -45,6 +46,18 @@ func TestKafkaEnvironmentsConfig(t *testing.T) {
}
}

func TestV0sConfigurationIsOptionalAndIndependent(t *testing.T) {
cfg, err := NewConfig(t.Context(), envconfig.MapLookuper(map[string]string{
"V0S_ENDPOINT": "v0s:8080", "VULNERABILITIES_ENDPOINT": "legacy:8080",
}))
if err != nil {
t.Fatal(err)
}
if cfg.V0s.Endpoint != "v0s:8080" || cfg.V0s.SuppressionsEnabled || cfg.VulnerabilitiesAPI.Endpoint != "legacy:8080" {
t.Fatalf("new reads changed the legacy endpoint or enabled writes: %+v, %+v", cfg.V0s, cfg.VulnerabilitiesAPI)
}
}

func TestInsecureWebhookURLsConfig(t *testing.T) {
for _, tt := range []struct {
name string
Expand Down
14 changes: 14 additions & 0 deletions internal/cmd/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import (
"github.com/nais/api/internal/environment"
"github.com/nais/api/internal/feature"
"github.com/nais/api/internal/github/repository"
"github.com/nais/api/internal/graph/apierror"
"github.com/nais/api/internal/graph/loader"
"github.com/nais/api/internal/grpc/grpcworkload"
"github.com/nais/api/internal/issue"
apik8s "github.com/nais/api/internal/kubernetes"
"github.com/nais/api/internal/kubernetes/crd"
Expand Down Expand Up @@ -58,6 +60,7 @@ import (
"github.com/nais/api/internal/user"
"github.com/nais/api/internal/usersync"
"github.com/nais/api/internal/utilization"
"github.com/nais/api/internal/v0s"
"github.com/nais/api/internal/vulnerability"
"github.com/nais/api/internal/workload"
"github.com/nais/api/internal/workload/application"
Expand All @@ -68,6 +71,7 @@ import (
"github.com/nais/api/internal/workload/podlog"
fakepodlog "github.com/nais/api/internal/workload/podlog/fake"
"github.com/nais/api/internal/workload/secret"
"github.com/nais/api/pkg/apiclient/protoapi"
"github.com/rs/cors"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
Expand Down Expand Up @@ -215,6 +219,7 @@ func ConfigureGraph(
aivenProjects aiven.Projects,
kafkaEnvironments kafkatopic.Environments,
vulnMgr *vulnerability.Manager,
v0sMgr *v0s.Manager,
tenantName string,
clusters []string,
hookdClient hookd.Client,
Expand Down Expand Up @@ -401,6 +406,15 @@ func ConfigureGraph(
ctx = activitylog.NewLoaderContext(ctx, pool)
ctx = event.NewLoaderContext(ctx, pool)
ctx = vulnerability.NewLoaderContext(ctx, vulnMgr, log)
ctx = v0s.WithManager(ctx, v0sMgr)
ctx = v0s.NewLoaderContext(ctx, func(ctx context.Context) (*protoapi.ListWorkloadImagesResponse, error) {
for _, env := range watchers.AppWatcher.EnvironmentNames() {
if !watchers.AppWatcher.HasSynced(env) || !watchers.JobWatcher.HasSynced(env) {
return nil, apierror.Errorf("Workload inventory is not ready; vulnerability coverage is unavailable.")
}
}
return grpcworkload.NewServer(watchers.AppWatcher, watchers.JobWatcher).ListImages(ctx, &protoapi.ListWorkloadImagesRequest{})
})
ctx = servicemaintenance.NewLoaderContext(ctx, serviceMaintenanceManager, log)
ctx = reconciler.NewLoaderContext(ctx, pool)
ctx = deployment.NewLoaderContext(ctx, pool, hookdClient)
Expand Down
8 changes: 8 additions & 0 deletions internal/graph/gengql/activitylog.generated.go

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

Loading
Loading