Skip to content

Commit

Permalink
Add Control Plane API call to add apikey for Iglu authentication (closes
Browse files Browse the repository at this point in the history
  • Loading branch information
aldemirenes committed Oct 7, 2017
1 parent f7e8b26 commit a71030c
Show file tree
Hide file tree
Showing 5 changed files with 417 additions and 0 deletions.
107 changes: 107 additions & 0 deletions provisioning/resources/control-plane/local_iglu.go
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 provisioning/resources/control-plane/local_iglu_test.go
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)
}
48 changes: 48 additions & 0 deletions provisioning/resources/control-plane/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func main() {
http.HandleFunc("/restart-services", restartServices)
http.HandleFunc("/enrichments", uploadEnrichments)
http.HandleFunc("/external-iglu", addExternalIgluServer)
http.HandleFunc("/local-iglu-apikey", addLocalIgluApikey)
log.Fatal(http.ListenAndServe(":10000", nil))
}

Expand Down Expand Up @@ -171,3 +172,50 @@ func addExternalIgluServer(resp http.ResponseWriter, req *http.Request) {
http.Error(resp, "", 404)
}
}

func addLocalIgluApikey(resp http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
req.ParseForm()

igluApikeyArr, checkApikey := req.Form["local_iglu_apikey"]
if !checkApikey {
http.Error(resp, "missing parameter", 400)
return
}
igluApikey := igluApikeyArr[0]

if !isValidUuid(igluApikey) {
http.Error(resp, "Given apikey is not valid UUID", 400)
return
}

psqlInfos := PsqlInfos{
User: config.Psql.User,
Password: config.Psql.Password,
Database: config.Psql.Database,
Addr: config.Psql.Addr,
}

localIglu := LocalIglu{
ConfigPath: config.Dirs.Config + "/" +
config.ConfigNames.IgluResolver,
IgluApikey: igluApikey,
Psql: psqlInfos,
}
err := localIglu.addApiKey()
if err != nil {
http.Error(resp, err.Error(), 500)
return
}

err = restartService("streamEnrich")
if err != nil {
http.Error(resp, err.Error(), 500)
return
}
resp.WriteHeader(http.StatusOK)
io.WriteString(resp, "added successfully")
} else {
http.Error(resp, "", 404)
}
}
2 changes: 2 additions & 0 deletions provisioning/resources/ui/js/components/ControlPlane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import axios from 'axios';
import RestartServicesSection from "./ControlPlaneComponents/RestartServices";
import UploadEnrichmentsForm from "./ControlPlaneComponents/UploadEnrichments";
import AddExternalIgluServerForm from "./ControlPlaneComponents/AddExternalIgluServer";
import AddLocalIgluApikeyForm from "./ControlPlaneComponents/AddLocalIgluApikey";

export class ControlPlane extends React.Component<{}, {}> {

Expand All @@ -32,6 +33,7 @@ export class ControlPlane extends React.Component<{}, {}> {
<RestartServicesSection />
<UploadEnrichmentsForm />
<AddExternalIgluServerForm />
<AddLocalIgluApikeyForm />
</div>
);
}
Expand Down
Loading

0 comments on commit a71030c

Please sign in to comment.