-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require different static token for using fleet-manager's Admin API
- Loading branch information
1 parent
2be6b60
commit cba2985
Showing
3 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package envtokenauth | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/stackrox/acs-fleet-manager/fleetshard/pkg/fleetmanager" | ||
) | ||
|
||
// Implements the Auth interface for simple static token based authentication | ||
// while fetching the token from a custom environment variable. | ||
type envTokenAuth struct { | ||
token string | ||
} | ||
|
||
// CreateAuth creates a new Auth instance which implements static token authentication | ||
// while fetching the token from the environment using the specified environment variable name. | ||
func CreateAuth(name string) (fleetmanager.Auth, error) { | ||
token := os.Getenv(name) | ||
if token == "" { | ||
return nil, fmt.Errorf("no token named %q found in current environment", name) | ||
} | ||
return &envTokenAuth{ | ||
token: token, | ||
}, nil | ||
} | ||
|
||
// AddAuth adds an Authorization header to the provided HTTP request. | ||
func (a *envTokenAuth) AddAuth(req *http.Request) error { | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", a.token)) | ||
return nil | ||
} |