Skip to content

Commit

Permalink
Add Control Plane API call to write domain name to Caddyfile for auto…
Browse files Browse the repository at this point in the history
…matic TLS (closes #118)
  • Loading branch information
aldemirenes authored and BenFradet committed Dec 12, 2017
1 parent a4dc77c commit 9bc99b4
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 0 deletions.
44 changes: 44 additions & 0 deletions provisioning/resources/control-plane/change_domain_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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 (
"io/ioutil"
)

func changeDomainName(configPath string, domainName string) error {
lines, err := fileToLines(configPath)
if err != nil {
return err
}

// write domain name to first line of Caddy config
lines[0] = domainName + " *:80 {"
// placeholder email address, not important
lines[1] = " tls [email protected]"

fileContent := ""
for _, line := range lines {
fileContent += line
fileContent += "\n"
}

return ioutil.WriteFile(configPath, []byte(fileContent), 0644)
}
75 changes: 75 additions & 0 deletions provisioning/resources/control-plane/change_domain_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* 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 TestChangeDomainName(t *testing.T) {
assert := assert.New(t)

caddyConfigHeadBefore :=
`*:80 {
tls off
basicauth username_test password_test {
/home
/kibana
/elasticsearch
/control-plane
/_plugin
}
`
expectedCaddyConfigHeadAfter :=
`example.com *:80 {
tls [email protected]
basicauth username_test password_test {
/home
/kibana
/elasticsearch
/control-plane
/_plugin
}
`
dir, err := ioutil.TempDir("", "testDir")
assert.Nil(err)

defer os.RemoveAll(dir)

tmpfn := filepath.Join(dir, "tmpfile")

err = ioutil.WriteFile(tmpfn, []byte(caddyConfigHeadBefore), 0666)
assert.Nil(err)

err = changeDomainName(
tmpfn,
"example.com",
)
assert.Nil(err)

caddyConfigAfter, err := ioutil.ReadFile(tmpfn)
assert.Nil(err)

assert.True(expectedCaddyConfigHeadAfter == string(caddyConfigAfter))
}
40 changes: 40 additions & 0 deletions provisioning/resources/control-plane/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func main() {
http.HandleFunc("/external-iglu", addExternalIgluServer)
http.HandleFunc("/local-iglu-apikey", addLocalIgluApikey)
http.HandleFunc("/credentials", changeUsernameAndPassword)
http.HandleFunc("/domain-name", addDomainName)
log.Fatal(http.ListenAndServe(":10000", nil))
}

Expand Down Expand Up @@ -256,3 +257,42 @@ func changeUsernameAndPassword(resp http.ResponseWriter, req *http.Request) {
http.Error(resp, "", 404)
}
}

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

domainNameArr, checkDomainName := req.Form["domain_name"]
if !checkDomainName {
http.Error(resp, "missing parameter", 400)
return
}
domainName := domainNameArr[0]

err := checkHostDomainName(domainName)
if err != nil {
http.Error(resp, err.Error(), 405)
return
}

err = changeDomainName(
config.Dirs.Config+"/"+config.ConfigNames.Caddy,
domainName,
)
if err != nil {
http.Error(resp, err.Error(), 405)
return
}

err = restartService("caddy")
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 @@ -24,6 +24,7 @@ import UploadEnrichmentsForm from "./ControlPlaneComponents/UploadEnrichments";
import AddExternalIgluServerForm from "./ControlPlaneComponents/AddExternalIgluServer";
import AddLocalIgluApikeyForm from "./ControlPlaneComponents/AddLocalIgluApikey";
import ChangeUsernamePasswordForm from "./ControlPlaneComponents/ChangeUsernamePassword";
import AddDomainNameForm from "./ControlPlaneComponents/AddDomainName";

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

Expand All @@ -36,6 +37,7 @@ export class ControlPlane extends React.Component<{}, {}> {
<AddExternalIgluServerForm />
<AddLocalIgluApikeyForm />
<ChangeUsernamePasswordForm />
<AddDomainNameForm />
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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 AlertContainer from 'react-alert';
import alertOptions from './AlertOptions'
import axios from 'axios';

var alertContainer = new AlertContainer();

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

handleChange(evt) {
if (evt.target.name == 'domain_name'){
this.setState({
domain_name: evt.target.value
});
}
},

sendFormData() {
var _this = this;
var alertShow = alertContainer.show
var domainName = this.state.domain_name

// there is no need to disabled false after
// because connection will be lost after request is sent
// and page will be loaded again
_this.setState({
disabled: true
});
var params = new URLSearchParams();
params.append('domain_name', _this.state.domain_name);

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.post('/control-plane/domain-name', params, {})
.then(function (response) {
// there is no need to this part because status will be
// 400 in everytime and this will be handled by catch section
})
.catch(function (error) {
alertShow("You will lose connection after change the username and \
password because of server restarting. Reload the page \
after submission and login with your new username and password.", {
time: 10000,
type: 'info'
});
});
},

handleSubmit(event) {
var alertShow = alertContainer.show
alertShow('Please wait...', {
time: 2000,
type: 'info'
});
event.preventDefault();
this.sendFormData();
},

render() {
return (
<div className="tab-content">
<h4>Add domain name for TLS: </h4>
<form action="" onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="domain_name">Domain name: </label>
<input className="form-control" name="domain_name" ref="domain_name" required type="text" onChange={this.handleChange} value={this.state.domain_name} />
</div>
<div className="form-group">
<button className="btn btn-primary" type="submit" disabled={this.state.disabled}>Submit</button>
</div>
</form>
<AlertContainer ref={a => alertContainer = a} {...alertOptions} />
</div>
);
}
});

0 comments on commit 9bc99b4

Please sign in to comment.