Skip to content

Commit ca1733a

Browse files
authored
Add debug providers command. (#3728)
When trying to run cosign on a remote workstation it would be nice to have a command to confirm the OIDC provider behavior of what is/isn't enabled and in what order. This PR adds a new hidden debug subcommand, with a debug providers subcommand that prints out the list of orderd providers and whether or not they are enabled. Signed-off-by: Billy Lynch <[email protected]>
1 parent 5bbccd5 commit ca1733a

File tree

4 files changed

+96
-14
lines changed

4 files changed

+96
-14
lines changed

cmd/cosign/cli/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func New() *cobra.Command {
9696
cmd.AddCommand(Attest())
9797
cmd.AddCommand(AttestBlob())
9898
cmd.AddCommand(Clean())
99+
cmd.AddCommand(Debug())
99100
cmd.AddCommand(Tree())
100101
cmd.AddCommand(Completion())
101102
cmd.AddCommand(Copy())

cmd/cosign/cli/debug.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2024 The Sigstore Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cli
16+
17+
import (
18+
"github.com/sigstore/cosign/v2/cmd/cosign/cli/debug"
19+
"github.com/spf13/cobra"
20+
)
21+
22+
func Debug() *cobra.Command {
23+
cmd := &cobra.Command{
24+
Use: "debug",
25+
Hidden: true,
26+
RunE: func(cmd *cobra.Command, _ []string) error {
27+
return cmd.Help()
28+
},
29+
}
30+
cmd.AddCommand(debugProviders())
31+
return cmd
32+
}
33+
34+
func debugProviders() *cobra.Command {
35+
cmd := &cobra.Command{
36+
Use: "providers",
37+
Short: "Show enabled/disabled OIDC providers.",
38+
RunE: func(cmd *cobra.Command, _ []string) error {
39+
return debug.ProviderCmd(cmd.Context(), cmd.OutOrStdout())
40+
},
41+
}
42+
return cmd
43+
}

cmd/cosign/cli/debug/provider.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2024 The Sigstore Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package debug
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"io"
21+
22+
"github.com/sigstore/cosign/v2/pkg/providers"
23+
)
24+
25+
func ProviderCmd(ctx context.Context, w io.Writer) error {
26+
for _, p := range providers.Providers() {
27+
fmt.Fprintf(w, "%s: %t\n", p.Name, p.Provider.Enabled(ctx))
28+
}
29+
return nil
30+
}

pkg/providers/interface.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import (
2424

2525
var (
2626
m sync.Mutex
27-
providers []providerEntry
27+
providers []ProviderEntry
2828
)
2929

30-
type providerEntry struct {
31-
name string
32-
p Interface
30+
type ProviderEntry struct {
31+
Name string
32+
Provider Interface
3333
}
3434

3535
// Interface is what providers need to implement to participate in furnishing OIDC tokens.
@@ -47,20 +47,20 @@ func Register(name string, p Interface) {
4747
defer m.Unlock()
4848

4949
for _, pe := range providers {
50-
if pe.name == name {
51-
panic(fmt.Sprintf("duplicate provider for name %q, %T and %T", name, pe.p, p))
50+
if pe.Name == name {
51+
panic(fmt.Sprintf("duplicate provider for name %q, %T and %T", name, pe.Provider, p))
5252
}
5353
}
54-
providers = append(providers, providerEntry{name: name, p: p})
54+
providers = append(providers, ProviderEntry{Name: name, Provider: p})
5555
}
5656

5757
// Enabled checks whether any of the registered providers are enabled in this execution context.
5858
func Enabled(ctx context.Context) bool {
5959
m.Lock()
6060
defer m.Unlock()
6161

62-
for _, provider := range providers {
63-
if provider.p.Enabled(ctx) {
62+
for _, pe := range providers {
63+
if pe.Provider.Enabled(ctx) {
6464
return true
6565
}
6666
}
@@ -74,11 +74,12 @@ func Provide(ctx context.Context, audience string) (string, error) {
7474

7575
var id string
7676
var err error
77-
for _, provider := range providers {
78-
if !provider.p.Enabled(ctx) {
77+
for _, pe := range providers {
78+
p := pe.Provider
79+
if !p.Enabled(ctx) {
7980
continue
8081
}
81-
id, err = provider.p.Provide(ctx, audience)
82+
id, err = p.Provide(ctx, audience)
8283
if err == nil {
8384
return id, nil
8485
}
@@ -97,9 +98,16 @@ func ProvideFrom(_ context.Context, provider string) (Interface, error) {
9798
defer m.Unlock()
9899

99100
for _, p := range providers {
100-
if p.name == provider {
101-
return p.p, nil
101+
if p.Name == provider {
102+
return p.Provider, nil
102103
}
103104
}
104105
return nil, fmt.Errorf("%s is not a valid provider", provider)
105106
}
107+
108+
func Providers() []ProviderEntry {
109+
m.Lock()
110+
defer m.Unlock()
111+
112+
return providers
113+
}

0 commit comments

Comments
 (0)