Compare commits
3 Commits
b6c9c5327e
...
50aad53e83
Author | SHA1 | Date |
---|---|---|
|
50aad53e83 | |
|
5db217219f | |
|
e1317ced9e |
|
@ -38,11 +38,11 @@ func NewIndexAddCmd(ctx context.Context, opt *options.Common) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "add [NAME] [URL] [BACKEND] [flags]",
|
Use: "add [NAME] [URL] [BACKEND] [TOKEN] [flags]",
|
||||||
DisableFlagsInUseLine: true,
|
DisableFlagsInUseLine: true,
|
||||||
Short: "Add an index to the local falcoctl configuration",
|
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",
|
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, 3),
|
Args: cobra.RangeArgs(2, 4),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return o.RunIndexAdd(ctx, args)
|
return o.RunIndexAdd(ctx, args)
|
||||||
},
|
},
|
||||||
|
@ -59,8 +59,12 @@ func (o *IndexAddOptions) RunIndexAdd(ctx context.Context, args []string) error
|
||||||
name := args[0]
|
name := args[0]
|
||||||
url := args[1]
|
url := args[1]
|
||||||
backend := ""
|
backend := ""
|
||||||
if len(args) > 2 {
|
token := ""
|
||||||
|
if len(args) == 3 {
|
||||||
backend = args[2]
|
backend = args[2]
|
||||||
|
} else if len(args) == 4 {
|
||||||
|
backend = args[2]
|
||||||
|
token = args[3]
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug("Creating in-memory cache using", logger.Args("indexes file", config.IndexesFile, "indexes directory", config.IndexesDir))
|
logger.Debug("Creating in-memory cache using", logger.Args("indexes file", config.IndexesFile, "indexes directory", config.IndexesDir))
|
||||||
|
@ -71,7 +75,7 @@ func (o *IndexAddOptions) RunIndexAdd(ctx context.Context, args []string) error
|
||||||
|
|
||||||
logger.Info("Adding index", logger.Args("name", name, "path", url))
|
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)
|
return fmt.Errorf("unable to add index: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ import (
|
||||||
|
|
||||||
//nolint:lll // no need to check for line length.
|
//nolint:lll // no need to check for line length.
|
||||||
var indexAddUsage = `Usage:
|
var indexAddUsage = `Usage:
|
||||||
falcoctl index add [NAME] [URL] [BACKEND] [flags]
|
falcoctl index add [NAME] [URL] [BACKEND] [TOKEN] [flags]
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
-h, --help help for add
|
-h, --help help for add
|
||||||
|
@ -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
|
var indexAddHelp = `Add an index to the local falcoctl configuration. Indexes are used to perform search operations for artifacts
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
falcoctl index add [NAME] [URL] [BACKEND] [flags]
|
falcoctl index add [NAME] [URL] [BACKEND] [TOKEN] [flags]
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
-h, --help help for add
|
-h, --help help for add
|
||||||
|
@ -97,7 +97,7 @@ var indexAddTests = Describe("add", func() {
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
args = []string{indexCmd, addCmd, "--config", configFile, indexName}
|
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() {
|
When("with invalid URL", func() {
|
||||||
|
|
|
@ -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
|
// 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
|
// 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.
|
// 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 remoteIndex *index.Index
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
@ -149,6 +149,7 @@ func (c *Cache) Add(ctx context.Context, name, backend, url string) error {
|
||||||
Name: name,
|
Name: name,
|
||||||
URL: url,
|
URL: url,
|
||||||
Backend: backend,
|
Backend: backend,
|
||||||
|
Token: token,
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the index is not locally cached we fetch it using the provided url.
|
// If the index is not locally cached we fetch it using the provided url.
|
||||||
|
@ -164,6 +165,7 @@ func (c *Cache) Add(ctx context.Context, name, backend, url string) error {
|
||||||
UpdatedTimestamp: ts,
|
UpdatedTimestamp: ts,
|
||||||
URL: url,
|
URL: url,
|
||||||
Backend: backend,
|
Backend: backend,
|
||||||
|
Token: token,
|
||||||
}
|
}
|
||||||
c.localIndexes.Add(entry)
|
c.localIndexes.Add(entry)
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@ type Entry struct {
|
||||||
UpdatedTimestamp string `yaml:"updated_timestamp"`
|
UpdatedTimestamp string `yaml:"updated_timestamp"`
|
||||||
URL string `yaml:"url"`
|
URL string `yaml:"url"`
|
||||||
Backend string `yaml:"backend"`
|
Backend string `yaml:"backend"`
|
||||||
|
Token string `yaml:"token"`
|
||||||
// TODO: add support for HTTP and other backend configs.
|
// TODO: add support for HTTP and other backend configs.
|
||||||
// HTTP http.BackendConfig `yaml:"http"`
|
// HTTP http.BackendConfig `yaml:"http"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,9 +17,11 @@ package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/falcosecurity/falcoctl/pkg/index/config"
|
"github.com/falcosecurity/falcoctl/pkg/index/config"
|
||||||
)
|
)
|
||||||
|
@ -31,6 +33,15 @@ func Fetch(ctx context.Context, conf *config.Entry) ([]byte, error) {
|
||||||
return nil, fmt.Errorf("cannot fetch index: %w", err)
|
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{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue