Skip to content

Commit

Permalink
feat(referrers): API endpoint (#424)
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
  • Loading branch information
migmartri authored Nov 9, 2023
1 parent 192e860 commit c4a6e12
Show file tree
Hide file tree
Showing 18 changed files with 1,800 additions and 48 deletions.
47 changes: 47 additions & 0 deletions app/cli/cmd/referrer_discover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Copyright 2023 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"context"

"github.com/chainloop-dev/chainloop/app/cli/internal/action"
"github.com/spf13/cobra"
)

func newReferrerDiscoverCmd() *cobra.Command {
var digest string

cmd := &cobra.Command{
Use: "discover",
Short: "(Preview) inspect pieces of evidence or artifacts stored through Chainloop",
RunE: func(cmd *cobra.Command, args []string) error {
res, err := action.NewReferrerDiscover(actionOpts).Run(context.Background(), digest)
if err != nil {
return err
}

// NOTE: this is a preview/beta command, for now we only return JSON format
return encodeJSON(res)
},
}

cmd.Flags().StringVarP(&digest, "digest", "d", "", "hash of the attestation, piece of evidence or artifact, i.e sha256:deadbeef")
err := cmd.MarkFlagRequired("digest")
cobra.CheckErr(err)

return cmd
}
1 change: 1 addition & 0 deletions app/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func NewRootCmd(l zerolog.Logger) *cobra.Command {
rootCmd.AddCommand(newWorkflowCmd(), newAuthCmd(), NewVersionCmd(),
newAttestationCmd(), newArtifactCmd(), newConfigCmd(),
newIntegrationCmd(), newOrganizationCmd(), newCASBackendCmd(),
newReferrerDiscoverCmd(),
)

return rootCmd
Expand Down
69 changes: 69 additions & 0 deletions app/cli/internal/action/referrer_discover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// Copyright 2023 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package action

import (
"context"
"time"

pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
)

type ReferrerDiscover struct {
cfg *ActionsOpts
}

type ReferrerItem struct {
Digest string `json:"digest"`
Kind string `json:"kind"`
Downloadable bool `json:"downloadable"`
CreatedAt *time.Time `json:"createdAt"`
References []*ReferrerItem `json:"references"`
}

func NewReferrerDiscover(cfg *ActionsOpts) *ReferrerDiscover {
return &ReferrerDiscover{cfg}
}

func (action *ReferrerDiscover) Run(ctx context.Context, digest string) (*ReferrerItem, error) {
client := pb.NewReferrerServiceClient(action.cfg.CPConnection)
resp, err := client.Discover(ctx, &pb.ReferrerServiceDiscoverRequest{Digest: digest})
if err != nil {
return nil, err
}

return pbReferrerItemToAction(resp.Result), nil
}

func pbReferrerItemToAction(in *pb.ReferrerItem) *ReferrerItem {
if in == nil {
return nil
}

out := &ReferrerItem{
Digest: in.GetDigest(),
Downloadable: in.GetDownloadable(),
Kind: in.GetKind(),
CreatedAt: toTimePtr(in.GetCreatedAt().AsTime()),
References: make([]*ReferrerItem, 0, len(in.GetReferences())),
}

for _, r := range in.GetReferences() {
out.References = append(out.References, pbReferrerItemToAction(r))
}

return out
}
Loading

0 comments on commit c4a6e12

Please sign in to comment.