-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
credential,endpoint: Split from pkg (#843)
* credential,endpoint: Split from pkg Signed-off-by: Xuanwo <github@xuanwo.io> * Update Makefile Signed-off-by: Xuanwo <github@xuanwo.io> * Fix filename Signed-off-by: Xuanwo <github@xuanwo.io>
- Loading branch information
Showing
28 changed files
with
1,053 additions
and
605 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Change Log | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/) | ||
and this project adheres to [Semantic Versioning](https://semver.org/). | ||
|
||
## [Unreleased] | ||
|
||
## v0.1.0 - 2021-09-27 | ||
|
||
- Migrate credential from go-storage to separate repo. | ||
- Add basic support |
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,36 @@ | ||
SHELL := /bin/bash | ||
|
||
.PHONY: all check format vet lint build test generate tidy integration_test | ||
|
||
help: | ||
@echo "Please use \`make <target>\` where <target> is one of" | ||
@echo " check to do static check" | ||
@echo " build to create bin directory and build" | ||
@echo " test to run test" | ||
|
||
check: vet | ||
|
||
format: | ||
@echo "go fmt" | ||
@go fmt ./... | ||
@echo "ok" | ||
|
||
vet: | ||
@echo "go vet" | ||
@go vet ./... | ||
@echo "ok" | ||
|
||
build: tidy check | ||
@echo "build go-credential" | ||
@go build ./... | ||
@echo "ok" | ||
|
||
test: | ||
@echo "run test" | ||
@go test -race -coverprofile=coverage.txt -covermode=atomic -v ./... | ||
@go tool cover -html="coverage.txt" -o "coverage.html" | ||
@echo "ok" | ||
|
||
tidy: | ||
@go mod tidy | ||
@go mod verify |
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,49 @@ | ||
# credential | ||
|
||
Both human and machine-readable credential format. | ||
|
||
## Format | ||
|
||
``` | ||
<protocol>:<value>+ | ||
``` | ||
|
||
For example: | ||
|
||
- hmac: `hmac:access_key:secret_key` | ||
- apikey: `apikey:apikey` | ||
- file: `file:/path/to/config/file` | ||
- basic: `basic:user:password` | ||
|
||
## Quick Start | ||
|
||
```go | ||
cred, err := credential.Parse("hmac:access_key:secret_key) | ||
if err != nil { | ||
log.Fatal("parse: ", err) | ||
} | ||
switch cred.Protocol() { | ||
case ProtocolHmac: | ||
ak, sk := cred.Hmac() | ||
log.Println("access_key: ", ak) | ||
log.Println("secret_key: ", sk) | ||
case ProtocolAPIKey: | ||
apikey := cred.APIKey() | ||
log.Println("apikey: ", apikey) | ||
case ProtocolFile: | ||
path := cred.File() | ||
log.Println("path: ", path) | ||
case ProtocolEnv: | ||
log.Println("use env value") | ||
case ProtocolBase64: | ||
content := cred.Base64() | ||
log.Println("base64: ", content) | ||
case ProtocolBasic: | ||
user, password := cred.Basic() | ||
log.Println("user: ", user) | ||
log.Println("password: ", password) | ||
default: | ||
panic("unsupported protocol") | ||
} | ||
``` |
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,115 @@ | ||
package credential | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestProvider(t *testing.T) { | ||
protocol := uuid.New().String() | ||
args := []string{uuid.New().String(), uuid.New().String()} | ||
|
||
p := Credential{protocol: protocol, args: args} | ||
|
||
assert.Equal(t, protocol, p.Protocol()) | ||
assert.EqualValues(t, args, p.Value()) | ||
} | ||
|
||
func TestParse(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
cfg string | ||
value Credential | ||
err error | ||
}{ | ||
{ | ||
"hmac", | ||
"hmac:ak:sk", | ||
Credential{protocol: ProtocolHmac, args: []string{"ak", "sk"}}, | ||
nil, | ||
}, | ||
{ | ||
"api key", | ||
"apikey:key", | ||
Credential{protocol: ProtocolAPIKey, args: []string{"key"}}, | ||
nil, | ||
}, | ||
{ | ||
"file", | ||
"file:/path/to/file", | ||
Credential{protocol: ProtocolFile, args: []string{"/path/to/file"}}, | ||
nil, | ||
}, | ||
{ | ||
"env", | ||
"env", | ||
Credential{protocol: ProtocolEnv}, | ||
nil, | ||
}, | ||
{ | ||
"base64", | ||
"base64:aGVsbG8sd29ybGQhCg==", | ||
Credential{protocol: ProtocolBase64, args: []string{"aGVsbG8sd29ybGQhCg=="}}, | ||
nil, | ||
}, | ||
{ | ||
"basic", | ||
"basic:user:password", | ||
Credential{protocol: ProtocolBasic, args: []string{"user", "password"}}, | ||
nil, | ||
}, | ||
{ | ||
"not supported protocol", | ||
"notsupported:ak:sk", | ||
Credential{}, | ||
ErrUnsupportedProtocol, | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
p, err := Parse(tt.cfg) | ||
if tt.err == nil { | ||
assert.Nil(t, err) | ||
} else { | ||
assert.True(t, errors.Is(err, tt.err)) | ||
} | ||
assert.EqualValues(t, tt.value, p) | ||
}) | ||
} | ||
} | ||
|
||
func ExampleParse() { | ||
cred, err := Parse("hmac:access_key:secret_key") | ||
if err != nil { | ||
log.Fatal("parse: ", err) | ||
} | ||
|
||
switch cred.Protocol() { | ||
case ProtocolHmac: | ||
ak, sk := cred.Hmac() | ||
log.Println("access_key: ", ak) | ||
log.Println("secret_key: ", sk) | ||
case ProtocolAPIKey: | ||
apikey := cred.APIKey() | ||
log.Println("apikey: ", apikey) | ||
case ProtocolFile: | ||
path := cred.File() | ||
log.Println("path: ", path) | ||
case ProtocolEnv: | ||
log.Println("use env value") | ||
case ProtocolBase64: | ||
content := cred.Base64() | ||
log.Println("base64: ", content) | ||
case ProtocolBasic: | ||
user, password := cred.Basic() | ||
log.Println("user: ", user) | ||
log.Println("password: ", password) | ||
default: | ||
panic("unsupported protocol") | ||
} | ||
} |
Oops, something went wrong.