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 Sep 18, 2017
1 parent 997895a commit 4f797cd
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
iglu_server_super_uid=$1
config_dir=$2

iglu_resolver_config_dir="$config_dir/iglu-resolver.json"

##there will be only one local iglu server, therefore apikey will be added for this iglu server
n=$(awk '/localhost/{print NR}' $iglu_resolver_config_dir) ## find number of the line which contains "localhost"
n=$((n+1)) ##add one to line number which contains 'localhost' keyword, this line will contain apikey for local iglu server
sudo sed -i ''$n's/\(.*"apikey":\)\(.*\)/\1 "'$iglu_server_super_uid'"/' $iglu_resolver_config_dir ##write apikey for local iglu server

#write super apikey to db
export PGPASSWORD=snowplow
delete_all_apikeys="DELETE FROM apikeys"
iglu_server_setup="INSERT INTO apikeys (uid, vendor_prefix, permission, createdat) VALUES ('${iglu_server_super_uid}','*','super',current_timestamp);"
psql --host=localhost --port=5432 --username=snowplow --dbname=iglu -c "${delete_all_apikeys}"
psql --host=localhost --port=5432 --username=snowplow --dbname=iglu -c "${iglu_server_setup}"
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
// script file names
var restartServicesScript = "restart_SP_services.sh"
var addExternalIgluServerScript = "add_external_iglu_server.sh"
var addIgluSuperUUIDScript = "add_iglu_server_super_uuid.sh"

// global variables for paths from flags
var scriptsPath string
Expand All @@ -49,6 +50,7 @@ func main() {
http.HandleFunc("/restart-services", restartSPServices)
http.HandleFunc("/upload-enrichments", uploadEnrichments)
http.HandleFunc("/add-external-iglu-server", addExternalIgluServer)
http.HandleFunc("/add-iglu-server-super-uuid", addIgluServerSuperUUID)
http.HandleFunc("/check-url", checkUrl)
log.Fatal(http.ListenAndServe(":10000", nil))
}
Expand Down Expand Up @@ -147,3 +149,35 @@ func addExternalIgluServer(resp http.ResponseWriter, req *http.Request) {
io.WriteString(resp, "added successfully")
}
}

func addIgluServerSuperUUID(resp http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
req.ParseForm()
if len(req.Form["iglu_server_super_uuid"]) == 0 {
http.Error(resp, "parameter iglu_server_super_uuid is not given", 400)
return
}
igluServerSuperUUID := req.Form["iglu_server_super_uuid"][0]
if !isValidUuid(igluServerSuperUUID) {
http.Error(resp, "Given apikey is not valid UUID", 400)
return
}
shellScriptCommand := []string{scriptsPath + "/" + addIgluSuperUUIDScript,
igluServerSuperUUID,
configPath}
cmd := exec.Command("/bin/bash", shellScriptCommand...)
err := cmd.Run()
if err != nil {
http.Error(resp, err.Error(), 400)
return
}
//restart SP services to get action the added Iglu apikey
_, err = callRestartSPServicesScript()
if err != nil {
http.Error(resp, err.Error(), 400)
return
}
resp.WriteHeader(http.StatusOK)
io.WriteString(resp, "added successfully")
}
}
28 changes: 28 additions & 0 deletions provisioning/resources/control-plane/test/test_rest_api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,34 @@ else
exit 1
fi

## add Iglu Server super uuid test: success
sudo cp $testEnv/orgConfig/iglu-resolver.json $testConfigDir/.
iglu_server_super_uuid=$(uuidgen)
add_super_uuid_result=$(curl -s -o /dev/null -w "%{http_code}" -d "iglu_server_super_uuid=$iglu_server_super_uuid" localhost:10000/add-iglu-server-super-uuid)
sleep 2

grep_res=$(cat $testConfigDir/iglu-resolver.json | grep $iglu_server_super_uuid)

if [[ "${add_super_uuid_result}" -eq 200 ]] && [[ "${grep_res}" != "" ]];then
echo "Adding Iglu Server super uuid success test returned as expected."
else
echo "Adding Iglu Server super uuid success test did not return as expected."
exit 1
fi

## add Iglu Server super uuid test: invalid UUID format fail
sudo cp $testEnv/orgConfig/iglu-resolver.json $testConfigDir/.
iglu_server_super_uuid="123"
add_super_uuid_result=$(curl -s -o /dev/null -w "%{http_code}" -d "iglu_server_super_uuid=$iglu_server_super_uuid" localhost:10000/add-iglu-server-super-uuid)
sleep 2

if [[ "${add_super_uuid_result}" -eq 400 ]];then
echo "Adding Iglu Server super uuid invalid UUID format fail test returned as expected."
else
echo "Adding Iglu Server super uuid invalid UUID format fail test did not return as expected."
exit 1
fi

sudo cp $testInit/snowplow_mini_control_plane_api_original_init /etc/init.d/snowplow_mini_control_plane_api
sudo /etc/init.d/snowplow_mini_control_plane_api restart

Expand Down
16 changes: 16 additions & 0 deletions provisioning/resources/control-plane/test/test_scripts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ else
exit 1
fi

## add Iglu Server super uuid test
sudo cp $testEnv/orgConfig/iglu-resolver.json $testConfigDir/.
iglu_server_super_uuid="04577adf-6dce-49d7-8cbb-0ffdf83304de"

sudo $scripts/$addIgluSuperUuidScript $iglu_server_super_uuid $testConfigDir >> /dev/null
res=$?

diff_test_expected=$(diff $testConfigDir/iglu-resolver.json $testEnv/expectedConfig/iglu-resolver-add-super-uuid.json)

if [[ "${res}" -eq 0 ]] && [[ "${diff_test_expected}" == "" ]];then
echo "Adding Iglu Server super uuid script is working correctly."
else
echo "Adding Iglu Server super uuid script is not working correctly."
exit 1
fi

#remove test control plane directory after testing is done
sudo rm -rf $testControlPlaneDir

Expand Down
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 AddIgluServerSuperUUIDForm from "./ControlPlaneComponents/AddIgluServerSuperUUID";

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

Expand All @@ -32,6 +33,7 @@ export class ControlPlane extends React.Component<{}, {}> {
<RestartServicesSection />
<UploadEnrichmentsForm />
<AddExternalIgluServerForm />
<AddIgluServerSuperUUIDForm />
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.
*/

/// <reference path="../../../typings/node/node.d.ts" />
/// <reference path="../../../typings/react/react.d.ts" />
/// <reference path="../../../typings/react/react-dom.d.ts" />
/// <reference path="../.././Interfaces.d.ts"/>

import React = require('react');
import ReactDOM = require("react-dom");
import axios from 'axios';

export default React.createClass({
getInitialState () {
return {
iglu_server_super_uuid: '',
disabled: false
};
},

handleChange(evt) {
this.setState({
iglu_server_super_uuid: evt.target.value
});
},

sendFormData() {
var _this = this
var igluServerSuperUUID = this.state.iglu_server_super_uuid

function setInitState() {
_this.setState({
iglu_server_uri: "",
iglu_server_apikey: "",
disabled: false
});
}

_this.setState({
disabled: true
});
var params = new URLSearchParams();
params.append('iglu_server_super_uuid', _this.state.iglu_server_super_uuid)
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
axios.post('/control-plane/add-iglu-server-super-uuid', params, {})
.then(function (response) {
setInitState()
alert('Uploaded successfully');
})
.catch(function (error) {
setInitState()
alert('Error:' + error.response.data);
});
},

handleSubmit(event) {
alert('Please wait...');
event.preventDefault();
this.sendFormData();
},

render() {
return (
<div className="tab-content">
<h4>Add super UUID for local Iglu Server: </h4>
<form action="" onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="iglu_server_super_uuid">Iglu Server Super UUID: </label>
<input className="form-control" name="iglu_server_super_uuid" ref="iglu_server_super_uuid" required type="text" onChange={this.handleChange} value={this.state.iglu_server_super_uuid} />
</div>
<div className="form-group">
<button className="btn btn-primary" type="submit" disabled={this.state.disabled}>Add UUID</button>
</div>
</form>
</div>
);
}
});

0 comments on commit 4f797cd

Please sign in to comment.