Skip to content

Commit c4a6e12

Browse files
authored
feat(referrers): API endpoint (#424)
Signed-off-by: Miguel Martinez Trivino <[email protected]>
1 parent 192e860 commit c4a6e12

File tree

18 files changed

+1800
-48
lines changed

18 files changed

+1800
-48
lines changed

app/cli/cmd/referrer_discover.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Copyright 2023 The Chainloop 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 cmd
17+
18+
import (
19+
"context"
20+
21+
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
22+
"github.com/spf13/cobra"
23+
)
24+
25+
func newReferrerDiscoverCmd() *cobra.Command {
26+
var digest string
27+
28+
cmd := &cobra.Command{
29+
Use: "discover",
30+
Short: "(Preview) inspect pieces of evidence or artifacts stored through Chainloop",
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
res, err := action.NewReferrerDiscover(actionOpts).Run(context.Background(), digest)
33+
if err != nil {
34+
return err
35+
}
36+
37+
// NOTE: this is a preview/beta command, for now we only return JSON format
38+
return encodeJSON(res)
39+
},
40+
}
41+
42+
cmd.Flags().StringVarP(&digest, "digest", "d", "", "hash of the attestation, piece of evidence or artifact, i.e sha256:deadbeef")
43+
err := cmd.MarkFlagRequired("digest")
44+
cobra.CheckErr(err)
45+
46+
return cmd
47+
}

app/cli/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func NewRootCmd(l zerolog.Logger) *cobra.Command {
110110
rootCmd.AddCommand(newWorkflowCmd(), newAuthCmd(), NewVersionCmd(),
111111
newAttestationCmd(), newArtifactCmd(), newConfigCmd(),
112112
newIntegrationCmd(), newOrganizationCmd(), newCASBackendCmd(),
113+
newReferrerDiscoverCmd(),
113114
)
114115

115116
return rootCmd
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// Copyright 2023 The Chainloop 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 action
17+
18+
import (
19+
"context"
20+
"time"
21+
22+
pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
23+
)
24+
25+
type ReferrerDiscover struct {
26+
cfg *ActionsOpts
27+
}
28+
29+
type ReferrerItem struct {
30+
Digest string `json:"digest"`
31+
Kind string `json:"kind"`
32+
Downloadable bool `json:"downloadable"`
33+
CreatedAt *time.Time `json:"createdAt"`
34+
References []*ReferrerItem `json:"references"`
35+
}
36+
37+
func NewReferrerDiscover(cfg *ActionsOpts) *ReferrerDiscover {
38+
return &ReferrerDiscover{cfg}
39+
}
40+
41+
func (action *ReferrerDiscover) Run(ctx context.Context, digest string) (*ReferrerItem, error) {
42+
client := pb.NewReferrerServiceClient(action.cfg.CPConnection)
43+
resp, err := client.Discover(ctx, &pb.ReferrerServiceDiscoverRequest{Digest: digest})
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
return pbReferrerItemToAction(resp.Result), nil
49+
}
50+
51+
func pbReferrerItemToAction(in *pb.ReferrerItem) *ReferrerItem {
52+
if in == nil {
53+
return nil
54+
}
55+
56+
out := &ReferrerItem{
57+
Digest: in.GetDigest(),
58+
Downloadable: in.GetDownloadable(),
59+
Kind: in.GetKind(),
60+
CreatedAt: toTimePtr(in.GetCreatedAt().AsTime()),
61+
References: make([]*ReferrerItem, 0, len(in.GetReferences())),
62+
}
63+
64+
for _, r := range in.GetReferences() {
65+
out.References = append(out.References, pbReferrerItemToAction(r))
66+
}
67+
68+
return out
69+
}

0 commit comments

Comments
 (0)