update token file location

This commit is contained in:
Wenkai Yin 2017-07-19 13:46:10 +08:00
parent 6e10a21871
commit 7573d59624
5 changed files with 7 additions and 76 deletions

View File

@ -83,7 +83,7 @@ services:
- ./common/config/ui/private_key.pem:/etc/ui/private_key.pem:z
- /data/secretkey:/etc/ui/key:z
- /data/ca_download/:/etc/ui/ca/:z
- /data/service_token:/etc/ui/service_token:z
- /etc/vmware/psc/harbor/:/etc/ui/token/:z
networks:
- harbor
depends_on:

View File

@ -77,7 +77,7 @@ services:
- ./common/config/ui/private_key.pem:/etc/ui/private_key.pem:z
- /data/secretkey:/etc/ui/key:z
- /data/ca_download/:/etc/ui/ca/:z
- /data/service_token:/etc/ui/service_token:z
- /etc/vmware/psc/harbor/:/etc/ui/token/:z
networks:
- harbor
depends_on:

View File

@ -36,7 +36,7 @@ import (
const (
defaultKeyPath string = "/etc/ui/key"
defaultTokenFilePath string = "/etc/ui/service_token"
defaultTokenFilePath string = "/etc/ui/token/tokens.properties"
secretCookieName string = "secret"
)

View File

@ -15,11 +15,7 @@
package pms
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"io/ioutil"
)
const (
@ -49,36 +45,9 @@ type FileTokenReader struct {
// ReadToken ...
func (f *FileTokenReader) ReadToken() (string, error) {
file, err := os.Open(f.Path)
data, err := ioutil.ReadFile(f.Path)
if err != nil {
return "", err
}
defer file.Close()
return readToken(file)
}
func readToken(reader io.Reader) (string, error) {
if reader == nil {
return "", fmt.Errorf("reader is nil")
}
r := bufio.NewReader(reader)
for {
line, _, err := r.ReadLine()
if err != nil {
if err == io.EOF {
err = fmt.Errorf("%s not found", key)
}
return "", err
}
strs := strings.SplitN(string(line), "=", 2)
if len(strs) != 2 {
continue
}
if strs[0] == key {
return strs[1], nil
}
}
return string(data), nil
}

View File

@ -15,7 +15,6 @@
package pms
import (
"bytes"
"io/ioutil"
"os"
"testing"
@ -35,43 +34,6 @@ func TestRawTokenReader(t *testing.T) {
assert.Equal(t, raw, token)
}
func TestReadToken(t *testing.T) {
// nil reader
_, err := readToken(nil)
assert.NotNil(t, err)
// empty
reader := bytes.NewReader([]byte{})
_, err = readToken(reader)
assert.NotNil(t, err)
// contains no "access_token"
content := "key1=value\nkey2=value2"
reader = bytes.NewReader([]byte(content))
_, err = readToken(reader)
assert.NotNil(t, err)
// contains "access_token" but no "="
content = "access_token value\nkey2=value2"
reader = bytes.NewReader([]byte(content))
_, err = readToken(reader)
assert.NotNil(t, err)
// contains "access_token" and "=", but no value
content = "access_token=\nkey2=value2"
reader = bytes.NewReader([]byte(content))
token, err := readToken(reader)
require.Nil(t, err)
assert.Len(t, token, 0)
// valid "access_token"
content = "access_token=token\nkey2=value2"
reader = bytes.NewReader([]byte(content))
token, err = readToken(reader)
require.Nil(t, err)
assert.Equal(t, "token", token)
}
func TestFileTokenReader(t *testing.T) {
// file not exist
path := "/tmp/not_exist_file"
@ -84,7 +46,7 @@ func TestFileTokenReader(t *testing.T) {
// file exist
path = "/tmp/exist_file"
err = ioutil.WriteFile(path, []byte("access_token=token"), 0x0666)
err = ioutil.WriteFile(path, []byte("token"), 0x0666)
require.Nil(t, err)
defer os.Remove(path)