Skip to content
Closed
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 .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ CCF_AGENT_MAX_INSTANCES=500
CCF_ARTIFACT_MAX_BYTES=16777216
CCF_ARTIFACT_MAX_CONCURRENT=8

# Evidence compliance badge
# Maximum arbitrary label query parameters for GET /api/evidence/badge.svg (positive integer).
CCF_EVIDENCE_BADGE_MAX_PARAMS=3

# Evidence subjects
# Agent evidence whose labels match no component subject template: off (accept), warn (accept
# and log) or enforce (reject with 400).
Expand Down
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,29 @@ Copy this file to .env to configure environment variables
cp .env.example .env
```

### Evidence compliance badge

`GET /api/evidence/badge.svg?provider=aws&environment=production` returns an SVG
badge using the latest evidence per stream matching **all** supplied labels.
Every query key is a label name; `source`, `repo`, and `branch` have no special
meaning, required format, or defaults. The one exception is `label`: it is reserved
for the repeatable `label=<name>:<value>` form (split at the first colon), e.g.
`?label=provider:aws&label=environment:production`. This form is what Swagger UI
sends, and it can be mixed with plain `name=value` labels. Send a label literally
named `label` as `label=label:<value>`.

Supply between one and `CCF_EVIDENCE_BADGE_MAX_PARAMS` labels (default **3**).
The setting must be a positive integer; an explicitly empty setting is invalid.
Empty names/values, repeated keys
(case-insensitive), and exceeding the limit return HTTP 400. Names and values
must be valid UTF-8, at most 255 characters, without surrounding whitespace or
control characters. Internal spaces are allowed; URL-encode label names and values.

The badge is red for `not-satisfied`, green for `satisfied` when no failures are
present, and grey for `unknown` (including no matching evidence). Responses use
`Cache-Control: no-store`. Existing evidence-read authentication and authorization
rules apply.

## Contributing

We welcome contributions to the API!
Expand Down Expand Up @@ -99,4 +122,3 @@ export TESTCONTAINERS_RYUK_DISABLED=true;

## License
This project is licensed under the GNU AGPLv3 License - see the [LICENSE](LICENSE) file for details.

4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
}
)

// setDefaultEnvironmentVariables configures defaults for supported Viper keys.
func setDefaultEnvironmentVariables() {
viper.SetDefault("app_port", ":8080")
viper.SetDefault("db_debug", "false")
Expand All @@ -48,6 +49,7 @@ func setDefaultEnvironmentVariables() {
viper.SetDefault("authz_fail_mode", "closed")
}

// bindEnvironmentVariables binds supported Viper keys to CCF-prefixed environment variables.
func bindEnvironmentVariables() {
viper.SetEnvPrefix("ccf")
viper.MustBindEnv("app_port")
Expand All @@ -69,6 +71,7 @@ func bindEnvironmentVariables() {
viper.MustBindEnv("metrics_port")
viper.MustBindEnv("use_dev_logger")
viper.MustBindEnv("evidence_default_expiry_months")
viper.MustBindEnv("evidence_badge_max_params")
viper.MustBindEnv("evidence_require_subject")
viper.MustBindEnv("manual_evidence_require_subject")
viper.MustBindEnv("digest_enabled")
Expand Down Expand Up @@ -149,6 +152,7 @@ func init() {
rootCmd.AddCommand(RiskDigestCmd)
}

// Execute runs the root API command.
func Execute() error {
if err := rootCmd.Execute(); err != nil {
fmt.Println("Error executing root command:", err)
Expand Down
25 changes: 25 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"time"

"github.com/compliance-framework/api/internal/config"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

// The config loaders below read the global viper, which only sees CCF_* environment
Expand All @@ -18,6 +20,7 @@ func TestBindEnvironmentVariables_ConfigLoadersReadEnv(t *testing.T) {
t.Setenv("CCF_EVIDENCE_BATCH_WORKERS", "8")
t.Setenv("CCF_EVIDENCE_BATCH_RETENTION", "72h")
t.Setenv("CCF_EVIDENCE_BATCH_PRUNE_SCHEDULE", "0 5 * * * *")
t.Setenv("CCF_EVIDENCE_BADGE_MAX_PARAMS", "7")
t.Setenv("CCF_SSP_SCOPE_AUTO_FILTERS_SCHEDULE", "0 6 * * * *")
t.Setenv("CCF_SSP_TEMPLATE_DISCOVERY_SCHEDULE", "0 7 * * * *")
t.Setenv("CCF_SSP_TEMPLATE_STALE_AFTER", "48h")
Expand All @@ -32,9 +35,31 @@ func TestBindEnvironmentVariables_ConfigLoadersReadEnv(t *testing.T) {
PruneSchedule: "0 5 * * * *",
}, batch)

badgeMaxParams, err := config.LoadEvidenceBadgeMaxParamsFromViper(viper.GetViper())
require.NoError(t, err)
require.Equal(t, 7, badgeMaxParams)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

require.Equal(t, &config.SSPScopeConfig{AutoFiltersSchedule: "0 6 * * * *"}, config.LoadSSPScopeConfig())

template, err := config.LoadSSPTemplateConfig()
require.NoError(t, err)
require.Equal(t, &config.SSPTemplateConfig{DiscoverySchedule: "0 7 * * * *", StaleAfter: 48 * time.Hour}, template)

t.Setenv("CCF_EVIDENCE_BADGE_MAX_PARAMS", "invalid")
_, err = config.LoadEvidenceBadgeMaxParamsFromViper(viper.GetViper())
require.ErrorContains(t, err, "CCF_EVIDENCE_BADGE_MAX_PARAMS")
}

// TestNewConfigEvidenceBadgeMaxParams verifies environment settings reach the application config.
func TestNewConfigEvidenceBadgeMaxParams(t *testing.T) {
t.Setenv("CCF_DB_DRIVER", "postgres")
t.Setenv("CCF_DB_CONNECTION", "host=localhost dbname=test")
t.Setenv("CCF_JWT_SECRET", "test-secret")
t.Setenv("CCF_JWT_PRIVATE_KEY", "")
t.Setenv("CCF_JWT_PUBLIC_KEY", "")
t.Setenv("CCF_EVIDENCE_BADGE_MAX_PARAMS", "7")

cfg := config.NewConfig(zap.NewNop().Sugar())

require.Equal(t, 7, cfg.EvidenceBadgeMaxParams)
}
63 changes: 63 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4827,6 +4827,69 @@ const docTemplate = `{
]
}
},
"/evidence/badge.svg": {
"get": {
"description": "Returns an SVG badge for the latest evidence matching ALL supplied query parameters as label name/value pairs, e.g. ?provider=aws\u0026environment=production.\nAggregates the latest evidence from each matching stream, so a failure in another stream can make the badge red (not-satisfied).\nRequires 1 to CCF_EVIDENCE_BADGE_MAX_PARAMS labels (default 3). Each key must occur exactly once (keys must be unique case-insensitively) with a non-empty value. No labels are implicit. Names and values are limited to 255 characters without surrounding whitespace or control characters; internal spaces are allowed.\nLabels can be sent in two equivalent forms, which may be mixed: one ?\u003clabel-name\u003e=\u003clabel-value\u003e query parameter per label, or the repeatable label parameter as label=\u003clabel-name\u003e:\u003clabel-value\u003e (split at the first colon). Swagger UI exposes only the label parameter; add one item per label pair.\nThe label key is reserved for the second form, so a label literally named \"label\" must be sent as label=label:\u003cvalue\u003e.\nAuthentication is conditional. When public evidence reads are enabled (CCF_STRICT_DISABLE_PUBLIC_AGENT_ENDPOINTS is false, the default) and the authorization policy permits the read, no credentials are required. Otherwise send an OAuth2 bearer token (Authorization: Bearer \u003ctoken\u003e) or the ccf_auth_token session cookie for a user or agent allowed to read evidence. A supplied Authorization header must always be valid.",
"produces": [
"image/svg+xml",
"application/json"
],
"tags": [
"Evidence"
],
"summary": "Get an SVG compliance status badge",
"parameters": [
{
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi",
"description": "Label pair as name:value, e.g. provider:aws. Repeat for each label.",
"name": "label",
"in": "query"
}
],
"responses": {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
"200": {
"description": "SVG compliance badge",
"schema": {
"type": "string"
}
},
"400": {
"description": "Missing, duplicate, too many or invalid label parameters",
"schema": {
"$ref": "#/definitions/api.Error"
}
},
"401": {
"description": "Credentials are required (public evidence reads disabled) or the supplied token is invalid",
"schema": {
"$ref": "#/definitions/api.Error"
}
},
"403": {
"description": "The authorization policy denies reading evidence",
"schema": {
"$ref": "#/definitions/api.Error"
}
},
"429": {
"description": "Too many badge requests in progress",
"schema": {
"$ref": "#/definitions/api.Error"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
Comment thread
alenmalhas marked this conversation as resolved.
Resolved
}
},
"/evidence/batches": {
"post": {
"description": "Accepts up to CCF_EVIDENCE_BATCH_MAX_ITEMS evidence and stores them asynchronously, returning the batch to poll with GET /evidence/batches/{id}. Each evidence is the body POST /evidence accepts; its shape and times are checked on submit, everything else when it is processed, where a failure fails that evidence only. Needs an authenticated user or agent, and the worker service.",
Expand Down
63 changes: 63 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -4821,6 +4821,69 @@
]
}
},
"/evidence/badge.svg": {
"get": {
"description": "Returns an SVG badge for the latest evidence matching ALL supplied query parameters as label name/value pairs, e.g. ?provider=aws\u0026environment=production.\nAggregates the latest evidence from each matching stream, so a failure in another stream can make the badge red (not-satisfied).\nRequires 1 to CCF_EVIDENCE_BADGE_MAX_PARAMS labels (default 3). Each key must occur exactly once (keys must be unique case-insensitively) with a non-empty value. No labels are implicit. Names and values are limited to 255 characters without surrounding whitespace or control characters; internal spaces are allowed.\nLabels can be sent in two equivalent forms, which may be mixed: one ?\u003clabel-name\u003e=\u003clabel-value\u003e query parameter per label, or the repeatable label parameter as label=\u003clabel-name\u003e:\u003clabel-value\u003e (split at the first colon). Swagger UI exposes only the label parameter; add one item per label pair.\nThe label key is reserved for the second form, so a label literally named \"label\" must be sent as label=label:\u003cvalue\u003e.\nAuthentication is conditional. When public evidence reads are enabled (CCF_STRICT_DISABLE_PUBLIC_AGENT_ENDPOINTS is false, the default) and the authorization policy permits the read, no credentials are required. Otherwise send an OAuth2 bearer token (Authorization: Bearer \u003ctoken\u003e) or the ccf_auth_token session cookie for a user or agent allowed to read evidence. A supplied Authorization header must always be valid.",
"produces": [
"image/svg+xml",
"application/json"
],
"tags": [
"Evidence"
],
"summary": "Get an SVG compliance status badge",
"parameters": [
{
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi",
"description": "Label pair as name:value, e.g. provider:aws. Repeat for each label.",
"name": "label",
"in": "query"
}
],
"responses": {
"200": {
"description": "SVG compliance badge",
"schema": {
"type": "string"
}
},
"400": {
"description": "Missing, duplicate, too many or invalid label parameters",
"schema": {
"$ref": "#/definitions/api.Error"
}
},
"401": {
"description": "Credentials are required (public evidence reads disabled) or the supplied token is invalid",
"schema": {
"$ref": "#/definitions/api.Error"
}
},
"403": {
"description": "The authorization policy denies reading evidence",
"schema": {
"$ref": "#/definitions/api.Error"
}
},
"429": {
"description": "Too many badge requests in progress",
"schema": {
"$ref": "#/definitions/api.Error"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
}
},
"/evidence/batches": {
"post": {
"description": "Accepts up to CCF_EVIDENCE_BATCH_MAX_ITEMS evidence and stores them asynchronously, returning the batch to poll with GET /evidence/batches/{id}. Each evidence is the body POST /evidence accepts; its shape and times are checked on submit, everything else when it is processed, where a failure fails that evidence only. Needs an authenticated user or agent, and the worker service.",
Expand Down
50 changes: 50 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16753,6 +16753,56 @@ paths:
summary: Verify Evidence signature by ID
tags:
- Evidence
/evidence/badge.svg:
get:
description: |-
Returns an SVG badge for the latest evidence matching ALL supplied query parameters as label name/value pairs, e.g. ?provider=aws&environment=production.
Aggregates the latest evidence from each matching stream, so a failure in another stream can make the badge red (not-satisfied).
Requires 1 to CCF_EVIDENCE_BADGE_MAX_PARAMS labels (default 3). Each key must occur exactly once (keys must be unique case-insensitively) with a non-empty value. No labels are implicit. Names and values are limited to 255 characters without surrounding whitespace or control characters; internal spaces are allowed.
Labels can be sent in two equivalent forms, which may be mixed: one ?<label-name>=<label-value> query parameter per label, or the repeatable label parameter as label=<label-name>:<label-value> (split at the first colon). Swagger UI exposes only the label parameter; add one item per label pair.
The label key is reserved for the second form, so a label literally named "label" must be sent as label=label:<value>.
Authentication is conditional. When public evidence reads are enabled (CCF_STRICT_DISABLE_PUBLIC_AGENT_ENDPOINTS is false, the default) and the authorization policy permits the read, no credentials are required. Otherwise send an OAuth2 bearer token (Authorization: Bearer <token>) or the ccf_auth_token session cookie for a user or agent allowed to read evidence. A supplied Authorization header must always be valid.
parameters:
- collectionFormat: multi
description: Label pair as name:value, e.g. provider:aws. Repeat for each
label.
in: query
items:
type: string
name: label
type: array
produces:
- image/svg+xml
- application/json
responses:
"200":
description: SVG compliance badge
schema:
type: string
"400":
description: Missing, duplicate, too many or invalid label parameters
schema:
$ref: '#/definitions/api.Error'
"401":
description: Credentials are required (public evidence reads disabled) or
the supplied token is invalid
schema:
$ref: '#/definitions/api.Error'
"403":
description: The authorization policy denies reading evidence
schema:
$ref: '#/definitions/api.Error'
"429":
description: Too many badge requests in progress
schema:
$ref: '#/definitions/api.Error'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/api.Error'
summary: Get an SVG compliance status badge
tags:
- Evidence
/evidence/batches:
post:
consumes:
Expand Down
3 changes: 3 additions & 0 deletions internal/api/handler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ func RegisterHandlers(server *api.Server, logger *zap.SugaredLogger, db *gorm.DB

artifactService := artifactsvc.NewService(db)
evidenceHandler := NewEvidenceHandler(logger, services.EvidenceService, riskService).WithArtifactService(artifactService)
if config.EvidenceBadgeMaxParams != 0 {
evidenceHandler.badgeMaxParams = config.EvidenceBadgeMaxParams
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
evidenceGuard := pep.For(authz.ResourceEvidence)

// Batches are stored in the background, signed as their submitter, and read back only
Expand Down
Loading