Skip to content

Commit c7e77ee

Browse files
authored
Support restricting kinds on insertion (#2814)
This adds a server flag to restrict the set of kinds that the server will support for insertion. Note that this does not affect the read path, meaning previously inserted entries are still readable. This will be used in the public instance to limit the set of kinds to the ones that are actively used to minimize the API attack surface. Signed-off-by: Hayden <[email protected]>
1 parent a10818a commit c7e77ee

6 files changed

Lines changed: 373 additions & 9 deletions

File tree

‎cmd/rekor-server/app/root.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/go-chi/chi/v5/middleware"
2828
"github.com/sigstore/rekor/pkg/api"
2929
"github.com/sigstore/rekor/pkg/log"
30+
"github.com/sigstore/rekor/pkg/types"
3031
cose "github.com/sigstore/rekor/pkg/types/cose/v0.0.1"
3132
intoto001 "github.com/sigstore/rekor/pkg/types/intoto/v0.0.1"
3233
intoto002 "github.com/sigstore/rekor/pkg/types/intoto/v0.0.2"
@@ -136,6 +137,11 @@ Memory and file-based signers should only be used for testing.`)
136137

137138
rootCmd.PersistentFlags().StringSlice("enabled_api_endpoints", operationIDs, "list of API endpoints to enable using operationId from openapi.yaml")
138139

140+
rootCmd.PersistentFlags().StringSlice("enabled_entry_types", types.ListSupportedKinds(),
141+
"comma-separated list of entry type kinds to enable on the server (e.g. rekord,intoto,dsse,hashedrekord). "+
142+
"If unset (default), all types compiled into the binary are enabled. Any kind specified here must be compiled "+
143+
"into the binary; unknown kinds will cause startup to fail.")
144+
139145
rootCmd.PersistentFlags().Uint64("max_request_body_size", 0, "maximum size for HTTP request body, in bytes; set to 0 for unlimited")
140146
rootCmd.PersistentFlags().Uint64("max_jar_metadata_size", 1048576, "maximum permitted size for jar META-INF/ files, in bytes; set to 0 for unlimited")
141147
rootCmd.PersistentFlags().Uint64("max_apk_metadata_size", 1048576, "maximum permitted size for apk .SIGN and .PKGINFO files, in bytes; set to 0 for unlimited")

‎cmd/rekor-server/app/serve.go‎

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ package app
1717

1818
import (
1919
"flag"
20+
"fmt"
2021
"net/http"
22+
"sort"
23+
"strings"
2124
"time"
2225

2326
"cloud.google.com/go/profiler"
@@ -32,6 +35,7 @@ import (
3235
"github.com/sigstore/rekor/pkg/generated/restapi"
3336
"github.com/sigstore/rekor/pkg/generated/restapi/operations"
3437
"github.com/sigstore/rekor/pkg/log"
38+
"github.com/sigstore/rekor/pkg/types"
3539
"github.com/sigstore/rekor/pkg/types/alpine"
3640
alpine_v001 "github.com/sigstore/rekor/pkg/types/alpine/v0.0.1"
3741
"github.com/sigstore/rekor/pkg/types/cose"
@@ -100,11 +104,9 @@ var serveCmd = &cobra.Command{
100104
log.Logger.Error(err)
101105
}
102106
}()
103-
//TODO: make this a config option for server to load via viper field
104-
//TODO: add command line option to print versions supported in binary
105-
106-
// these trigger loading of package and therefore init() methods to run
107-
pluggableTypeMap := map[string][]string{
107+
// These trigger loading of the package and therefore init() methods to run,
108+
// which registers each type's constructor in types.TypeMap.
109+
entryTypeMap := map[string][]string{
108110
rekord.KIND: {rekord_v001.APIVERSION},
109111
rpm.KIND: {rpm_v001.APIVERSION},
110112
jar.KIND: {jar_v001.APIVERSION},
@@ -117,9 +119,20 @@ var serveCmd = &cobra.Command{
117119
hashedrekord.KIND: {hashedrekord_v001.APIVERSION},
118120
dsse.KIND: {dsse_v001.APIVERSION},
119121
}
120-
for k, v := range pluggableTypeMap {
121-
log.Logger.Infof("Loading support for pluggable type '%v'", k)
122-
log.Logger.Infof("Loading version '%v' for pluggable type '%v'", v, k)
122+
123+
// Using --enabled_entry_types, restrict which kinds the server will
124+
// accept for new submissions. By default, support every type compiled
125+
// into the binary.
126+
requestedTypes := viper.GetStringSlice("enabled_entry_types")
127+
enabledTypes, err := filterEntryTypes(entryTypeMap, requestedTypes)
128+
if err != nil {
129+
log.Logger.Fatalf("invalid --enabled_entry_types flag: %v", err)
130+
}
131+
types.SetAllowedKindsForSubmission(enabledTypes)
132+
133+
for _, k := range enabledTypes {
134+
log.Logger.Infof("Loading support for entry type '%v'", k)
135+
log.Logger.Infof("Loading version '%v' for entry type '%v'", entryTypeMap[k], k)
123136
}
124137

125138
server.Host = viper.GetString("rekor_server.address")
@@ -150,3 +163,29 @@ var serveCmd = &cobra.Command{
150163
func init() {
151164
rootCmd.AddCommand(serveCmd)
152165
}
166+
167+
// filterEntryTypes filters the list of compiled-in entry types
168+
// down to the set requested via the --enabled_entry_types flag.
169+
// If any requested type is not present, an error is returned.
170+
// Note that filtering only affects the write path, not the read paths.
171+
func filterEntryTypes(allTypes map[string][]string, requestedTypes []string) ([]string, error) {
172+
enabled := make(map[string]struct{})
173+
for _, kind := range requestedTypes {
174+
_, ok := allTypes[kind]
175+
if !ok {
176+
known := make([]string, 0, len(allTypes))
177+
for k := range allTypes {
178+
known = append(known, k)
179+
}
180+
sort.Strings(known)
181+
return nil, fmt.Errorf("unknown entry type %q; compiled-in types are: %s",
182+
kind, strings.Join(known, ", "))
183+
}
184+
enabled[kind] = struct{}{}
185+
}
186+
enabledKinds := make([]string, 0, len(enabled))
187+
for k := range enabled {
188+
enabledKinds = append(enabledKinds, k)
189+
}
190+
return enabledKinds, nil
191+
}

‎cmd/rekor-server/app/serve_test.go‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
//
2+
// Copyright 2026 The Sigstore Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package app
17+
18+
import (
19+
"reflect"
20+
"sort"
21+
"strings"
22+
"testing"
23+
)
24+
25+
func TestFilterEntryTypes_EmptyRequestedReturnsNothing(t *testing.T) {
26+
t.Parallel()
27+
allEntries := map[string][]string{
28+
"rekord": {"0.0.1"},
29+
"intoto": {"0.0.1", "0.0.2"},
30+
"hashedrekord": {"0.0.1"},
31+
}
32+
33+
got, err := filterEntryTypes(allEntries, nil)
34+
if err != nil {
35+
t.Fatalf("unexpected error: %v", err)
36+
}
37+
if len(got) != 0 {
38+
t.Errorf("nil requested should return no entries")
39+
}
40+
41+
got, err = filterEntryTypes(allEntries, []string{})
42+
if err != nil {
43+
t.Fatalf("unexpected error: %v", err)
44+
}
45+
if len(got) != 0 {
46+
t.Errorf("empty slice requested should return no entries")
47+
}
48+
}
49+
50+
func TestFilterEntryTypes_SubsetPreservesVersions(t *testing.T) {
51+
t.Parallel()
52+
allEntries := map[string][]string{
53+
"rekord": {"0.0.1"},
54+
"intoto": {"0.0.1", "0.0.2"},
55+
"hashedrekord": {"0.0.1"},
56+
}
57+
58+
got, err := filterEntryTypes(allEntries, []string{"rekord", "intoto"})
59+
if err != nil {
60+
t.Fatalf("unexpected error: %v", err)
61+
}
62+
63+
want := []string{"rekord", "intoto"}
64+
sort.Strings(got)
65+
sort.Strings(want)
66+
if !reflect.DeepEqual(got, want) {
67+
t.Errorf("subset filter mismatch\n got: %v\nwant: %v", got, want)
68+
}
69+
}
70+
71+
func TestFilterEntryTypes_UnknownKindErrors(t *testing.T) {
72+
t.Parallel()
73+
allEntries := map[string][]string{
74+
"rekord": {"0.0.1"},
75+
"intoto": {"0.0.1", "0.0.2"},
76+
}
77+
78+
_, err := filterEntryTypes(allEntries, []string{"rekord", "doesnotexist"})
79+
if err == nil {
80+
t.Fatal("expected error for unknown kind, got nil")
81+
}
82+
msg := err.Error()
83+
if !strings.Contains(msg, `"doesnotexist"`) {
84+
t.Errorf("error should quote the bad kind, got: %v", err)
85+
}
86+
for _, want := range []string{"rekord", "intoto"} {
87+
if !strings.Contains(msg, want) {
88+
t.Errorf("error should list known kind %q, got: %v", want, err)
89+
}
90+
}
91+
}
92+
93+
func TestFilterEntryTypes_KnownKindsListedSorted(t *testing.T) {
94+
t.Parallel()
95+
allEntries := map[string][]string{
96+
"zeta": {"0.0.1"},
97+
"alpha": {"0.0.1"},
98+
"mu": {"0.0.1"},
99+
}
100+
101+
_, err := filterEntryTypes(allEntries, []string{"bogus"})
102+
if err == nil {
103+
t.Fatal("expected error")
104+
}
105+
msg := err.Error()
106+
a, m, z := strings.Index(msg, "alpha"), strings.Index(msg, "mu"), strings.Index(msg, "zeta")
107+
if a == -1 || m == -1 || z == -1 {
108+
t.Fatalf("error message missing kinds: %s", msg)
109+
}
110+
if a >= m || m >= z {
111+
t.Errorf("expected sorted order alpha < mu < zeta in error, got: %s", msg)
112+
}
113+
}
114+
115+
func TestFilterEntryTypes_DuplicatesAreIdempotent(t *testing.T) {
116+
t.Parallel()
117+
allEntries := map[string][]string{
118+
"rekord": {"0.0.1"},
119+
"intoto": {"0.0.1", "0.0.2"},
120+
}
121+
122+
got, err := filterEntryTypes(allEntries, []string{"rekord", "rekord", "intoto"})
123+
if err != nil {
124+
t.Fatalf("unexpected error: %v", err)
125+
}
126+
want := []string{"rekord", "intoto"}
127+
sort.Strings(got)
128+
sort.Strings(want)
129+
if !reflect.DeepEqual(got, want) {
130+
t.Errorf("duplicate kinds should de-dupe\n got: %v\nwant: %v", got, want)
131+
}
132+
}

‎pkg/types/entries.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"net/url"
2424
"reflect"
25+
"sync/atomic"
2526

2627
"github.com/cyberphone/json-canonicalization/go/src/webpki.org/jsoncanonicalizer"
2728
"github.com/go-openapi/strfmt"
@@ -60,6 +61,30 @@ type ProposedEntryIterator interface {
6061
// EntryFactory describes a factory function that can generate structs for a specific versioned type
6162
type EntryFactory func() EntryImpl
6263

64+
// allowedKindsForSubmission restricts the set of kinds that CreateVersionedEntry
65+
// will accept.
66+
// This restriction only applies to the insertion path. Read paths are unaffected
67+
// so that entries written to the log under a previous configuration are still readable.
68+
var allowedKindsForSubmission atomic.Pointer[map[string]struct{}]
69+
70+
// SetAllowedKindsForSubmission configures the set of kinds that CreateVersionedEntry
71+
// will accept.
72+
func SetAllowedKindsForSubmission(kinds []string) {
73+
m := make(map[string]struct{}, len(kinds))
74+
for _, k := range kinds {
75+
m[k] = struct{}{}
76+
}
77+
allowedKindsForSubmission.Store(&m)
78+
}
79+
80+
// isKindAllowedForSubmission reports whether the given kind may be inserted
81+
// into the log via CreateVersionedEntry.
82+
func isKindAllowedForSubmission(kind string) bool {
83+
m := allowedKindsForSubmission.Load()
84+
_, ok := (*m)[kind]
85+
return ok
86+
}
87+
6388
func NewProposedEntry(ctx context.Context, kind, version string, props ArtifactProperties) (models.ProposedEntry, error) {
6489
if tf, found := TypeMap.Load(kind); found {
6590
t := tf.(func() TypeImpl)()
@@ -80,6 +105,9 @@ func CreateVersionedEntry(pe models.ProposedEntry) (EntryImpl, error) {
80105
return nil, err
81106
}
82107
kind := pe.Kind()
108+
if !isKindAllowedForSubmission(kind) {
109+
return nil, fmt.Errorf("entry kind '%v' is not enabled for submission on this server", kind)
110+
}
83111
if tf, found := TypeMap.Load(kind); found {
84112
if !tf.(func() TypeImpl)().IsSupportedVersion(ei.APIVersion()) {
85113
return nil, fmt.Errorf("entry kind '%v' does not support inserting entries of version '%v'", kind, ei.APIVersion())

0 commit comments

Comments
 (0)