Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added authentication functionality for the private index. #635

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 8 additions & 5 deletions cmd/index/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func NewIndexAddCmd(ctx context.Context, opt *options.Common) *cobra.Command {
}

cmd := &cobra.Command{
Use: "add [NAME] [URL] [BACKEND] [flags]",
Use: "add [NAME] [URL] [BACKEND] [TOKEN] [flags]",
DisableFlagsInUseLine: true,
Short: "Add an index to the local falcoctl configuration",
Long: "Add an index to the local falcoctl configuration. Indexes are used to perform search operations for artifacts",
Args: cobra.RangeArgs(2, 3),
Long: "Add an index to the local falcoctl configuration. Indexes are used to perform search operations for artifacts\nIf you need authentication for using private index. You have to use token ( base64 encode \"HeaderName:Token\" )",
Args: cobra.RangeArgs(2, 4),
RunE: func(cmd *cobra.Command, args []string) error {
return o.RunIndexAdd(ctx, args)
},
Expand All @@ -59,8 +59,11 @@ func (o *IndexAddOptions) RunIndexAdd(ctx context.Context, args []string) error
name := args[0]
url := args[1]
backend := ""
if len(args) > 2 {
token := ""
if len(args) == 3 {
backend = args[2]
} else if len(args) == 4 {
token = args[3]
}

logger.Debug("Creating in-memory cache using", logger.Args("indexes file", config.IndexesFile, "indexes directory", config.IndexesDir))
Expand All @@ -71,7 +74,7 @@ func (o *IndexAddOptions) RunIndexAdd(ctx context.Context, args []string) error

logger.Info("Adding index", logger.Args("name", name, "path", url))

if err = indexCache.Add(ctx, name, backend, url); err != nil {
if err = indexCache.Add(ctx, name, backend, url, token); err != nil {
return fmt.Errorf("unable to add index: %w", err)
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/index/add/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

//nolint:lll // no need to check for line length.
var indexAddUsage = `Usage:
falcoctl index add [NAME] [URL] [BACKEND] [flags]
falcoctl index add [NAME] [URL] [BACKEND] [TOKEN] [flags]

Flags:
-h, --help help for add
Expand All @@ -42,7 +42,7 @@ Global Flags:
var indexAddHelp = `Add an index to the local falcoctl configuration. Indexes are used to perform search operations for artifacts

Usage:
falcoctl index add [NAME] [URL] [BACKEND] [flags]
falcoctl index add [NAME] [URL] [BACKEND] [TOKEN] [flags]

Flags:
-h, --help help for add
Expand Down Expand Up @@ -97,7 +97,7 @@ var indexAddTests = Describe("add", func() {
BeforeEach(func() {
args = []string{indexCmd, addCmd, "--config", configFile, indexName}
})
addAssertFailedBehavior(indexAddUsage, "ERROR accepts between 2 and 3 arg(s), received 1")
addAssertFailedBehavior(indexAddUsage, "ERROR accepts between 2 and 4 arg(s), received 1")
})

When("with invalid URL", func() {
Expand Down
4 changes: 3 additions & 1 deletion pkg/index/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func NewFromConfig(ctx context.Context, indexFile, indexesDir string, indexes []
// Add adds a new index file to the cache. If the index file already exists in the cache it
// does nothing. On the other hand, it fetches the index file using the provided URL and adds
// it to the in memory cache. It does not write it to the filesystem. It is idempotent.
func (c *Cache) Add(ctx context.Context, name, backend, url string) error {
func (c *Cache) Add(ctx context.Context, name, backend, url, token string) error {
var remoteIndex *index.Index
var err error

Expand All @@ -149,6 +149,7 @@ func (c *Cache) Add(ctx context.Context, name, backend, url string) error {
Name: name,
URL: url,
Backend: backend,
Token: token,
}

// If the index is not locally cached we fetch it using the provided url.
Expand All @@ -164,6 +165,7 @@ func (c *Cache) Add(ctx context.Context, name, backend, url string) error {
UpdatedTimestamp: ts,
URL: url,
Backend: backend,
Token: token,
}
c.localIndexes.Add(entry)

Expand Down
1 change: 1 addition & 0 deletions pkg/index/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Entry struct {
UpdatedTimestamp string `yaml:"updated_timestamp"`
URL string `yaml:"url"`
Backend string `yaml:"backend"`
Token string `yaml:"token"`
// TODO: add support for HTTP and other backend configs.
// HTTP http.BackendConfig `yaml:"http"`
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/index/fetch/http/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ package http

import (
"context"
"encoding/base64"
"fmt"
"io"
"net/http"
"strings"

"github.com/falcosecurity/falcoctl/pkg/index/config"
)
Expand All @@ -31,6 +33,15 @@ func Fetch(ctx context.Context, conf *config.Entry) ([]byte, error) {
return nil, fmt.Errorf("cannot fetch index: %w", err)
}

if conf.Token != "" {
tokenString, err := base64.StdEncoding.DecodeString(conf.Token)
if err != nil {
return nil, fmt.Errorf("unable to parse index token: %w", err)
}
indexToken := strings.Split(string(tokenString), ":")
req.Header.Add(indexToken[0], indexToken[1])
}

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
Expand Down