Skip to content
Draft
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
16 changes: 3 additions & 13 deletions cmd/kosli/evaluateHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,14 @@ func (o *commonEvaluateOptions) assertOnDeny() bool {
return !o.noAssert
}

func fetchAndEnrichTrail(flowName, trailName string, attestations []string) (any, error) {
trailURL, err := url.JoinPath(global.Host, "api/v2/trails", global.Org, flowName, trailName)
if err != nil {
return nil, err
}

reqParams := &requests.RequestParams{
Method: http.MethodGet,
URL: trailURL,
Token: global.ApiToken,
}
response, err := kosliClient.Do(reqParams)
func fetchAndEnrichTrail(ctx context.Context, flowName, trailName string, attestations []string) (any, error) {
trail, err := kosliAPI().GetTrail(ctx, flowName, trailName)
if err != nil {
return nil, err
}

var trailData any
err = json.Unmarshal([]byte(response.Body), &trailData)
err = json.Unmarshal(trail.Raw, &trailData)
if err != nil {
return nil, fmt.Errorf("failed to parse trail response: %v", err)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/kosli/evaluateTrail.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"io"

"github.com/kosli-dev/cli/internal/evaluations"
Expand Down Expand Up @@ -103,7 +104,7 @@ func newEvaluateTrailCmd(out io.Writer) *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(out, args)
return o.run(cmd.Context(), out, args)
},
}

Expand All @@ -118,13 +119,13 @@ func newEvaluateTrailCmd(out io.Writer) *cobra.Command {
return cmd
}

func (o *evaluateTrailOptions) run(out io.Writer, args []string) error {
func (o *evaluateTrailOptions) run(ctx context.Context, out io.Writer, args []string) error {
if o.serverSide {
return evaluateServerSide(out, &o.commonEvaluateOptions,
[]evaluations.TrailRef{{Flow: o.flowName, Trail: args[0]}})
}

trailData, err := fetchAndEnrichTrail(o.flowName, args[0], o.attestations)
trailData, err := fetchAndEnrichTrail(ctx, o.flowName, args[0], o.attestations)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/kosli/evaluateTrails.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"io"

"github.com/kosli-dev/cli/internal/evaluations"
Expand Down Expand Up @@ -96,7 +97,7 @@ func newEvaluateTrailsCmd(out io.Writer) *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(out, args)
return o.run(cmd.Context(), out, args)
},
}

Expand All @@ -111,7 +112,7 @@ func newEvaluateTrailsCmd(out io.Writer) *cobra.Command {
return cmd
}

func (o *evaluateTrailsOptions) run(out io.Writer, args []string) error {
func (o *evaluateTrailsOptions) run(ctx context.Context, out io.Writer, args []string) error {
if o.serverSide {
refs := make([]evaluations.TrailRef, 0, len(args))
for _, trailName := range args {
Expand All @@ -122,7 +123,7 @@ func (o *evaluateTrailsOptions) run(out io.Writer, args []string) error {

var trails []any
for _, trailName := range args {
trailData, err := fetchAndEnrichTrail(o.flowName, trailName, o.attestations)
trailData, err := fetchAndEnrichTrail(ctx, o.flowName, trailName, o.attestations)
if err != nil {
return err
}
Expand Down
21 changes: 5 additions & 16 deletions cmd/kosli/getTrail.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package main

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"sort"
"strings"

"github.com/kosli-dev/cli/internal/output"
"github.com/kosli-dev/cli/internal/requests"
"github.com/spf13/cobra"
)

Expand All @@ -36,7 +35,7 @@ func newGetTrailCmd(out io.Writer) *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(out, args)
return o.run(cmd.Context(), out, args)
},
}

Expand All @@ -51,23 +50,13 @@ func newGetTrailCmd(out io.Writer) *cobra.Command {
return cmd
}

func (o *getTrailOptions) run(out io.Writer, args []string) error {
url, err := url.JoinPath(global.Host, "api/v2/trails", global.Org, o.flowName, args[0])
func (o *getTrailOptions) run(ctx context.Context, out io.Writer, args []string) error {
trail, err := kosliAPI().GetTrail(ctx, o.flowName, args[0])
if err != nil {
return err
}

reqParams := &requests.RequestParams{
Method: http.MethodGet,
URL: url,
Token: global.ApiToken,
}
response, err := kosliClient.Do(reqParams)
if err != nil {
return err
}

return output.FormattedPrint(response.Body, o.output, out, 0,
return output.FormattedPrint(string(trail.Raw), o.output, out, 0,
map[string]output.FormatOutputFunc{
"table": printTrailAsTable,
"json": output.PrintJson,
Expand Down
12 changes: 12 additions & 0 deletions cmd/kosli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"

"github.com/kosli-dev/cli/internal/kosli"
log "github.com/kosli-dev/cli/internal/logger"
"github.com/kosli-dev/cli/internal/requests"
"github.com/kosli-dev/cli/internal/version"
Expand All @@ -25,6 +26,17 @@ func init() {
kosliClient, _ = requests.NewKosliClient("", 3, false, logger)
}

// kosliAPI returns a Kosli API client for the global flags as they are now.
// It is built on every call rather than once, so that multi-host runs and
// tests that set global directly each get a client for their own values.
func kosliAPI() *kosli.Client {
var sender kosli.Sender = kosliClient
if global.DryRun {
sender = kosli.DryRun(sender)
}
return kosli.New(sender, global.Host, global.Org, global.ApiToken)
}

func main() {
var err error
if isMultiHost() {
Expand Down
3 changes: 2 additions & 1 deletion internal/kosli/client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Package kosli is a client for the Kosli API, with one file per tag of the
// API's OpenAPI spec.
// API's OpenAPI spec. Each endpoint method's godoc names the operationId it
// calls, so the hand-written methods can be matched against the spec.
package kosli

import (
Expand Down
18 changes: 18 additions & 0 deletions internal/kosli/trails.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kosli

import (
"context"
"net/http"

"github.com/kosli-dev/cli/internal/requests"
)

// GetTrail calls get_trail. It returns the Trail called name in flow,
// undecoded: its callers either print the body or reshape it as arbitrary JSON.
func (c *Client) GetTrail(ctx context.Context, flow, name string) (*Result, error) {
endpoint, err := c.endpoint("trails", c.org, flow, name)
if err != nil {
return nil, err
}
return c.send(ctx, &requests.RequestParams{Method: http.MethodGet, URL: endpoint})
}
41 changes: 41 additions & 0 deletions internal/kosli/trails_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package kosli

import (
"context"
"net/http"
"testing"

"github.com/stretchr/testify/require"
)

func TestGetTrailRequestsTheTrailInTheOrgAndReturnsTheRawBody(t *testing.T) {
server, seen := newFakeServer(t, http.StatusOK, `{"name":"trail 1","compliance_status":{}}`)
client := newTestClient(t, server.URL)

result, err := client.GetTrail(context.Background(), "flow-1", "trail 1")

require.NoError(t, err)
require.Equal(t, http.MethodGet, seen.method)
require.Equal(t, "/api/v2/trails/test-org/flow-1/trail%201", seen.rawPath)
require.JSONEq(t, `{"name":"trail 1","compliance_status":{}}`, string(result.Raw))
}

func TestGetTrailOfAMissingTrailIsNotFound(t *testing.T) {
server, _ := newFakeServer(t, http.StatusNotFound, `{"message":"Trail not found"}`)
client := newTestClient(t, server.URL)

_, err := client.GetTrail(context.Background(), "flow-1", "missing")

require.True(t, IsNotFound(err))
Comment thread
dangrondahl marked this conversation as resolved.
}

func TestGetTrailInADryRunStillReadsTheTrail(t *testing.T) {
server, seen := newFakeServer(t, http.StatusOK, `{"name":"trail-1"}`)
client := New(DryRun(newHTTPClient(t)), server.URL, "test-org", "test-token")

result, err := client.GetTrail(context.Background(), "flow-1", "trail-1")

require.NoError(t, err)
require.Equal(t, 1, seen.hits)
require.JSONEq(t, `{"name":"trail-1"}`, string(result.Raw))
}
Loading