-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Control Plane API call to add apikey for Iglu authentication (closes
#116)
- Loading branch information
1 parent
30657e0
commit 7c840ad
Showing
5 changed files
with
417 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* Copyright (c) 2016-2017 Snowplow Analytics Ltd. | ||
* All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache | ||
* License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at | ||
* http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. | ||
* | ||
* See the Apache License Version 2.0 for the specific language | ||
* governing permissions and limitations there under. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"gopkg.in/pg.v5" | ||
"io/ioutil" | ||
"strings" | ||
) | ||
|
||
type PsqlInfos struct { | ||
User string | ||
Password string | ||
Database string | ||
Addr string | ||
} | ||
|
||
type LocalIglu struct { | ||
ConfigPath string | ||
IgluApikey string | ||
Psql PsqlInfos | ||
} | ||
|
||
func (li LocalIglu) addApiKeyToConfig() error { | ||
|
||
jsonFile, err := ioutil.ReadFile(li.ConfigPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var igluConf IgluConf | ||
json.Unmarshal(jsonFile, &igluConf) | ||
|
||
for i, repo := range igluConf.Data.Repos { | ||
igluUri := repo.Conn.Http.Uri | ||
if strings.Contains(igluUri, "localhost") || | ||
strings.Contains(igluUri, "127.0.0.1") { | ||
|
||
igluConf.Data.Repos[i].Conn.Http.Apikey = li.IgluApikey | ||
} | ||
} | ||
|
||
jsonBytes, err := json.MarshalIndent(igluConf, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ioutil.WriteFile(li.ConfigPath, jsonBytes, 0644) | ||
} | ||
|
||
func (li LocalIglu) insertApiKeyToDb() error { | ||
db := pg.Connect(&pg.Options{ | ||
User: li.Psql.User, | ||
Password: li.Psql.Password, | ||
Database: li.Psql.Database, | ||
Addr: li.Psql.Addr, | ||
}) | ||
defer db.Close() | ||
|
||
_, err := db.Exec("DELETE FROM apikeys") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = db.Exec("INSERT INTO apikeys " + | ||
"(uid, vendor_prefix, permission, createdat) " + | ||
"VALUES " + | ||
"('" + li.IgluApikey + "','*','super',current_timestamp)") | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (li LocalIglu) addApiKey() error { | ||
err := li.addApiKeyToConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = li.insertApiKeyToDb() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
157 changes: 157 additions & 0 deletions
157
provisioning/resources/control-plane/local_iglu_test.go
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,157 @@ | ||
/** | ||
* Copyright (c) 2016-2017 Snowplow Analytics Ltd. | ||
* All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache | ||
* License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at | ||
* http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. | ||
* | ||
* See the Apache License Version 2.0 for the specific language | ||
* governing permissions and limitations there under. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestAddApiKeyToConfig(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
igluConfigOne := | ||
`{ | ||
"schema": "iglu:com.snowplowanalytics.iglu/resolver-config/jsonschema/1-0-1", | ||
"data": { | ||
"cacheSize": 500, | ||
"repositories": [ | ||
{ | ||
"name": "Iglu Server", | ||
"priority": 0, | ||
"vendorPrefixes": [ | ||
"com.snowplowanalytics" | ||
], | ||
"connection": { | ||
"http": { | ||
"uri": "http://localhost:8081/api", | ||
"apikey": "PLACEHOLDER" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}` | ||
expectedIgluConfigOne := | ||
`{ | ||
"schema": "iglu:com.snowplowanalytics.iglu/resolver-config/jsonschema/1-0-1", | ||
"data": { | ||
"cacheSize": 500, | ||
"repositories": [ | ||
{ | ||
"name": "Iglu Server", | ||
"priority": 0, | ||
"vendorPrefixes": [ | ||
"com.snowplowanalytics" | ||
], | ||
"connection": { | ||
"http": { | ||
"uri": "http://localhost:8081/api", | ||
"apikey": "iglu_apikey" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}` | ||
|
||
igluConfigTwo := | ||
`{ | ||
"schema": "iglu:com.snowplowanalytics.iglu/resolver-config/jsonschema/1-0-1", | ||
"data": { | ||
"cacheSize": 500, | ||
"repositories": [ | ||
{ | ||
"name": "Iglu Server", | ||
"priority": 0, | ||
"vendorPrefixes": [ | ||
"com.snowplowanalytics" | ||
], | ||
"connection": { | ||
"http": { | ||
"uri": "http://localhost:8081/api" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}` | ||
|
||
expectedIgluConfigTwo := | ||
`{ | ||
"schema": "iglu:com.snowplowanalytics.iglu/resolver-config/jsonschema/1-0-1", | ||
"data": { | ||
"cacheSize": 500, | ||
"repositories": [ | ||
{ | ||
"name": "Iglu Server", | ||
"priority": 0, | ||
"vendorPrefixes": [ | ||
"com.snowplowanalytics" | ||
], | ||
"connection": { | ||
"http": { | ||
"uri": "http://localhost:8081/api", | ||
"apikey": "iglu_apikey" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}` | ||
|
||
dir, err := ioutil.TempDir("", "testDir") | ||
assert.Nil(err) | ||
|
||
defer os.RemoveAll(dir) | ||
|
||
tmpfn := filepath.Join(dir, "tmpfile") | ||
|
||
localIglu := LocalIglu{ | ||
ConfigPath: tmpfn, | ||
IgluApikey: "iglu_apikey", | ||
Psql: PsqlInfos{}, | ||
} | ||
|
||
err = ioutil.WriteFile(tmpfn, []byte(igluConfigOne), 0666) | ||
assert.Nil(err) | ||
|
||
err = localIglu.addApiKeyToConfig() | ||
assert.Nil(err) | ||
|
||
afterInsert, err := ioutil.ReadFile(tmpfn) | ||
assert.Nil(err) | ||
|
||
assert.True(string(afterInsert) == expectedIgluConfigOne) | ||
|
||
err = ioutil.WriteFile(tmpfn, []byte(igluConfigTwo), 0666) | ||
assert.Nil(err) | ||
|
||
err = localIglu.addApiKeyToConfig() | ||
assert.Nil(err) | ||
|
||
afterInsert, err = ioutil.ReadFile(tmpfn) | ||
assert.Nil(err) | ||
|
||
assert.True(string(afterInsert) == expectedIgluConfigTwo) | ||
} |
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
Oops, something went wrong.