pkg/util: Check errors (#19832)

* pkg/util: Check errors
* pkg/services: DRY up code
This commit is contained in:
Arve Knudsen
2019-10-23 10:40:12 +02:00
committed by GitHub
parent 31a346fcf2
commit 35e0e078b7
30 changed files with 247 additions and 84 deletions
+23 -10
View File
@@ -21,20 +21,30 @@ type ApiKeyJson struct {
OrgId int64 `json:"id"`
}
func New(orgId int64, name string) KeyGenResult {
jsonKey := ApiKeyJson{}
func New(orgId int64, name string) (KeyGenResult, error) {
result := KeyGenResult{}
jsonKey := ApiKeyJson{}
jsonKey.OrgId = orgId
jsonKey.Name = name
jsonKey.Key = util.GetRandomString(32)
var err error
jsonKey.Key, err = util.GetRandomString(32)
if err != nil {
return result, err
}
result := KeyGenResult{}
result.HashedKey = util.EncodePassword(jsonKey.Key, name)
result.HashedKey, err = util.EncodePassword(jsonKey.Key, name)
if err != nil {
return result, err
}
jsonString, _ := json.Marshal(jsonKey)
jsonString, err := json.Marshal(jsonKey)
if err != nil {
return result, err
}
result.ClientSecret = base64.StdEncoding.EncodeToString(jsonString)
return result
return result, nil
}
func Decode(keyString string) (*ApiKeyJson, error) {
@@ -52,7 +62,10 @@ func Decode(keyString string) (*ApiKeyJson, error) {
return &keyObj, nil
}
func IsValid(key *ApiKeyJson, hashedKey string) bool {
check := util.EncodePassword(key.Key, key.Name)
return check == hashedKey
func IsValid(key *ApiKeyJson, hashedKey string) (bool, error) {
check, err := util.EncodePassword(key.Key, key.Name)
if err != nil {
return false, err
}
return check == hashedKey, nil
}
+4 -2
View File
@@ -10,7 +10,8 @@ import (
func TestApiKeyGen(t *testing.T) {
Convey("When generating new api key", t, func() {
result := New(12, "Cool key")
result, err := New(12, "Cool key")
So(err, ShouldBeNil)
So(result.ClientSecret, ShouldNotBeEmpty)
So(result.HashedKey, ShouldNotBeEmpty)
@@ -19,7 +20,8 @@ func TestApiKeyGen(t *testing.T) {
keyInfo, err := Decode(result.ClientSecret)
So(err, ShouldBeNil)
keyHashed := util.EncodePassword(keyInfo.Key, keyInfo.Name)
keyHashed, err := util.EncodePassword(keyInfo.Key, keyInfo.Name)
So(err, ShouldBeNil)
So(keyHashed, ShouldEqual, result.HashedKey)
})
})
@@ -51,7 +51,12 @@ func (az *AzureBlobUploader) Upload(ctx context.Context, imageDiskPath string) (
}
defer file.Close()
randomFileName := util.GetRandomString(30) + ".png"
randomFileName, err := util.GetRandomString(30)
if err != nil {
return "", err
}
randomFileName += pngExt
// upload image
az.log.Debug("Uploading image to azure_blob", "container_name", az.container_name, "blob_name", randomFileName)
resp, err := blob.FileUpload(az.container_name, randomFileName, file)
+6 -1
View File
@@ -35,7 +35,12 @@ func NewGCSUploader(keyFile, bucket, path string) *GCSUploader {
}
func (u *GCSUploader) Upload(ctx context.Context, imageDiskPath string) (string, error) {
fileName := util.GetRandomString(20) + ".png"
fileName, err := util.GetRandomString(20)
if err != nil {
return "", err
}
fileName += pngExt
key := path.Join(u.path, fileName)
u.log.Debug("Opening key file ", u.keyFile)
@@ -9,6 +9,8 @@ import (
"github.com/grafana/grafana/pkg/setting"
)
const pngExt = ".png"
type ImageUploader interface {
Upload(ctx context.Context, path string) (string, error)
}
+5 -1
View File
@@ -61,7 +61,11 @@ func (u *S3Uploader) Upload(ctx context.Context, imageDiskPath string) (string,
}
s3_endpoint, _ := endpoints.DefaultResolver().EndpointFor("s3", u.region)
key := u.path + util.GetRandomString(20) + ".png"
rand, err := util.GetRandomString(20)
if err != nil {
return "", err
}
key := u.path + rand + pngExt
image_url := s3_endpoint.URL + "/" + u.bucket + "/" + key
log.Debug("Uploading image to s3. url = %s", image_url)
+6 -1
View File
@@ -47,7 +47,12 @@ func (u *WebdavUploader) PublicURL(filename string) string {
func (u *WebdavUploader) Upload(ctx context.Context, pa string) (string, error) {
url, _ := url.Parse(u.url)
filename := util.GetRandomString(20) + ".png"
filename, err := util.GetRandomString(20)
if err != nil {
return "", err
}
filename += pngExt
url.Path = path.Join(url.Path, filename)
imgData, err := ioutil.ReadFile(pa)