Skip to content

Commit c62dbc4

Browse files
tfadeyioluwole.fadeyi
andauthored
Remove basic auth from agent codebase (#195)
* Remove basic auth from agent codebase Removes basic auth from the preflight client inialization. Still allows preflight client to be not auth. Related #414 Signed-off-by: Oluwole Fadeyi <[email protected]> * Remove token field from the agent config Removed token field from agent config as it isn't used anymore. Related [#414] Signed-off-by: Oluwole Fadeyi <[email protected]> Co-authored-by: oluwole.fadeyi <[email protected]>
1 parent e9f2ff3 commit c62dbc4

File tree

6 files changed

+24
-56
lines changed

6 files changed

+24
-56
lines changed

cmd/agent.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ func init() {
3737
"./agent.yaml",
3838
"Config file location, default is `agent.yaml` in the current working directory.",
3939
)
40-
agentCmd.PersistentFlags().StringVarP(
41-
&agent.AuthToken,
42-
"auth-token",
43-
"t",
44-
"",
45-
"Authorization token. If used, it will override the authorization token in the configuration file.",
46-
)
4740
agentCmd.PersistentFlags().DurationVarP(
4841
&agent.Period,
4942
"period",

pkg/agent/config.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ import (
1818
// Config wraps the options for a run of the agent.
1919
type Config struct {
2020
Schedule string `yaml:"schedule"`
21-
// Token is the agent token if using basic authentication.
22-
// If not provided it will assume OAuth2 authentication.
23-
Token string `yaml:"token"`
2421
// Deprecated: Endpoint is being replaced with Server.
2522
Endpoint Endpoint `yaml:"endpoint"`
2623
// Server is the base url for the Preflight server.
@@ -130,13 +127,11 @@ func (c *Config) Dump() (string, error) {
130127
func (c *Config) validate() error {
131128
var result *multierror.Error
132129

133-
if c.Token == "" {
134-
if c.OrganizationID == "" {
135-
result = multierror.Append(result, fmt.Errorf("organization_id is required"))
136-
}
137-
if c.ClusterID == "" {
138-
result = multierror.Append(result, fmt.Errorf("cluster_id is required"))
139-
}
130+
if c.OrganizationID == "" {
131+
result = multierror.Append(result, fmt.Errorf("organization_id is required"))
132+
}
133+
if c.ClusterID == "" {
134+
result = multierror.Append(result, fmt.Errorf("cluster_id is required"))
140135
}
141136

142137
if c.Server != "" {

pkg/agent/config_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111

1212
func TestValidConfigLoad(t *testing.T) {
1313
configFileContents := `
14-
token: "12345"
1514
server: "http://localhost:8080"
15+
organization_id: "example"
16+
cluster_id: "example-cluster"
1617
data-gatherers:
1718
- name: d1
1819
kind: dummy
@@ -28,8 +29,9 @@ func TestValidConfigLoad(t *testing.T) {
2829
}
2930

3031
expected := Config{
31-
Token: "12345",
32-
Server: "http://localhost:8080",
32+
Server: "http://localhost:8080",
33+
OrganizationID: "example",
34+
ClusterID: "example-cluster",
3335
DataGatherers: []dataGatherer{
3436
dataGatherer{
3537
Name: "d1",
@@ -54,7 +56,8 @@ func TestValidConfigWithEndpointLoad(t *testing.T) {
5456
host: example.com
5557
path: api/v1/data
5658
schedule: "* * * * *"
57-
token: "12345"
59+
organization_id: "example"
60+
cluster_id: "example-cluster"
5861
data-gatherers:
5962
- name: d1
6063
kind: dummy
@@ -73,8 +76,9 @@ func TestValidConfigWithEndpointLoad(t *testing.T) {
7376
Host: "example.com",
7477
Path: "api/v1/data",
7578
},
76-
Schedule: "* * * * *",
77-
Token: "12345",
79+
Schedule: "* * * * *",
80+
OrganizationID: "example",
81+
ClusterID: "example-cluster",
7882
DataGatherers: []dataGatherer{
7983
dataGatherer{
8084
Name: "d1",
@@ -132,7 +136,8 @@ func TestPartialMissingConfigError(t *testing.T) {
132136
host: example.com
133137
path: /api/v1/data
134138
schedule: "* * * * *"
135-
token: "12345"
139+
organization_id: "example"
140+
cluster_id: "example-cluster"
136141
data-gatherers:
137142
- kind: dummy`))
138143

@@ -189,7 +194,6 @@ func TestInvalidDataGathered(t *testing.T) {
189194
host: example.com
190195
path: /api/v1/data
191196
schedule: "* * * * *"
192-
token: "12345"
193197
data-gatherers:
194198
- kind: "foo"`))
195199

pkg/agent/run.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ import (
2424
// ConfigFilePath is where the agent will try to load the configuration from
2525
var ConfigFilePath string
2626

27-
// AuthToken is the authorization token that will be used for API calls
28-
var AuthToken string
29-
3027
// Period is the time waited between scans
3128
var Period time.Duration
3229

@@ -73,17 +70,6 @@ func getConfiguration(ctx context.Context) (Config, *client.PreflightClient) {
7370
log.Fatalf("Failed to parse config file: %s", err)
7471
}
7572

76-
// AuthToken flag takes preference over token in configuration file.
77-
if AuthToken == "" {
78-
AuthToken = config.Token
79-
} else {
80-
log.Printf("Using authorization token from flag.")
81-
}
82-
83-
if config.Token != "" {
84-
config.Token = "(redacted)"
85-
}
86-
8773
baseURL := config.Server
8874
if baseURL == "" {
8975
log.Printf("Using deprecated Endpoint configuration. User Server instead.")
@@ -128,11 +114,8 @@ func getConfiguration(ctx context.Context) (Config, *client.PreflightClient) {
128114
log.Fatalf("Error creating preflight client: %+v", err)
129115
}
130116
} else {
131-
if AuthToken == "" {
132-
log.Fatalf("Missing authorization token. Cannot continue.")
133-
}
134-
135-
preflightClient, err = client.NewWithBasicAuth(agentMetadata, AuthToken, baseURL)
117+
log.Printf("No credentials file was specified. Starting client with no authentication...")
118+
preflightClient, err = client.NewWithNoAuth(agentMetadata, baseURL)
136119
if err != nil {
137120
log.Fatalf("Error creating preflight client: %+v", err)
138121
}

pkg/client/client.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,18 @@ type PreflightClient struct {
2424

2525
baseURL string
2626

27-
// basicAuthToken will be used instead of using OAuth2 based authentication if userID is not set.
28-
// It can be empty, meaning that no authentication will be used.
29-
basicAuthToken string
30-
3127
agentMetadata *api.AgentMetadata
3228
}
3329

34-
// NewWithBasicAuth creates a new client with basic authentication.
35-
func NewWithBasicAuth(agentMetadata *api.AgentMetadata, authToken, baseURL string) (*PreflightClient, error) {
30+
// NewWithNoAuth creates a new client with no authentication.
31+
func NewWithNoAuth(agentMetadata *api.AgentMetadata, baseURL string) (*PreflightClient, error) {
3632
if baseURL == "" {
3733
return nil, fmt.Errorf("cannot create PreflightClient: baseURL cannot be empty")
3834
}
3935

4036
return &PreflightClient{
41-
agentMetadata: agentMetadata,
42-
basicAuthToken: authToken,
43-
baseURL: baseURL,
37+
agentMetadata: agentMetadata,
38+
baseURL: baseURL,
4439
}, nil
4540
}
4641

pkg/client/http.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import (
1010
// Post performs a post request.
1111
func (c *PreflightClient) Post(path string, body io.Reader) (*http.Response, error) {
1212
var bearer string
13-
if !c.usingOAuth2() {
14-
bearer = c.basicAuthToken
15-
} else {
13+
if c.usingOAuth2() {
1614
token, err := c.getValidAccessToken()
1715
if err != nil {
1816
return nil, err

0 commit comments

Comments
 (0)