Skip to content

Commit f5c7215

Browse files
authored
#nosmoke Add organization plugin (#479)
* added company plugin to janus to pair users with a company * changed plugin term from company to organization, updated plugin to handle authentication separete from basic auth plugin. * fixed wrong log type. updated debug entry script to start janus properly * added page to doc
1 parent 8422787 commit f5c7215

File tree

12 files changed

+1412
-875
lines changed

12 files changed

+1412
-875
lines changed

cassandra/schema.sql

+6
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ CREATE TABLE IF NOT EXISTS janus.oauth (
1515
name text,
1616
oauth text,
1717
PRIMARY KEY (name));
18+
19+
CREATE TABLE IF NOT EXISTS janus.organization (
20+
username text,
21+
password text,
22+
organization text,
23+
PRIMARY KEY (username));

cassandra/wrapper/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func createAppKeyspaceIfRequired(clusterHostName, systemKeyspace, appKeyspace st
213213
// execute statement
214214
err = session.Query(stmt).Exec()
215215
if err != nil {
216-
log.Error("statement error: %v", err)
216+
log.Errorf("statement error: %v", err)
217217
return err
218218
}
219219
log.Debug("Statement executed")

cmd/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
_ "github.com/hellofresh/janus/pkg/plugin/compression"
1717
_ "github.com/hellofresh/janus/pkg/plugin/cors"
1818
_ "github.com/hellofresh/janus/pkg/plugin/oauth2"
19+
_ "github.com/hellofresh/janus/pkg/plugin/organization"
1920
_ "github.com/hellofresh/janus/pkg/plugin/rate"
2021
_ "github.com/hellofresh/janus/pkg/plugin/requesttransformer"
2122
_ "github.com/hellofresh/janus/pkg/plugin/responsetransformer"

docs/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* [Conclusion](proxy/conclusion.md)
2828
* [Plugins](plugins/README.md)
2929
* [Basic](plugins/basic.md)
30+
* [Organization](plugins/organization_auth.md)
3031
* [Body Limit](plugins/body_limit.md)
3132
* [Circuit Breaker](plugins/cb.md)
3233
* [Compression](plugins/compression.md)

docs/plugins/organization_auth.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Organization Auth
2+
3+
Create users with organizations and add an organization header to upstream requests.
4+
The plugin works similarly to basic auth with the exception that it also tracks an organization for users.
5+
It will also add the organization of the users to the header of upstream requests.
6+
7+
**Limitations**
8+
1. This plugin only works as a Basic Authentication not Oauth.
9+
2. This plugin only works with Cassandra DB repo.
10+
11+
## Configuration
12+
13+
The plain organization header config:
14+
15+
```json
16+
{
17+
"name": "organization_header",
18+
"enabled": true
19+
}
20+
```
21+
22+
Here is a simple definition of the available configurations.
23+
24+
| Configuration | Description |
25+
|-------------------------------|---------------------------------------------------------------------|
26+
| name | Name of the plugin to use, in this case: organization_header |
27+
| enabled | Is the plugin enabled? |
28+
29+
## Usage
30+
31+
You need to create an user that will be used to authenticate. To create an user you can execute the following request:
32+
33+
{% codetabs name="HTTPie", type="bash" -%}
34+
http -v POST http://localhost:8081/credentials/basic_auth "Authorization:Bearer yourToken" username=lanister password=pay-your-debt organization=motiv
35+
{%- language name="CURL", type="bash" -%}
36+
curl -X POST http://localhost:8081/credentials/basic_auth -H 'authorization: Bearer yourToken' -H 'content-type: application/json' -d '{"username": "lanister", "password": "pay-your-debt", "organization": "motiv"}'
37+
{%- endcodetabs %}
38+
39+
| FORM PARAMETER | Description |
40+
|----------------|-------------------------------------------------|
41+
| username | The username to use in the Basic Authentication |
42+
| password | The password to use in the Basic Authentication |
43+
| organization | The organization of the user |
44+
45+
## Using the Credential
46+
47+
The authorization header must be base64 encoded. For example, if the credential uses `lanister` as the username and `pay-your-debt` as the password, then the field's value is the base64-encoding of lanister:pay-your-debt, or bGFuaXN0ZXI6cGF5LXlvdXItZGVidA==.
48+
49+
Then the `Authorization` header must appear as:
50+
51+
Authorization: Basic bGFuaXN0ZXI6cGF5LXlvdXItZGVidA==
52+
Simply make a request with the header:
53+
54+
{% codetabs name="HTTPie", type="bash" -%}
55+
http -v http://localhost:8080/example "Authorization:Basic bGFuaXN0ZXI6cGF5LXlvdXItZGVidA=="
56+
{%- language name="CURL", type="bash" -%}
57+
curl -v http://localhost:8080/example -H 'Authorization:Basic bGFuaXN0ZXI6cGF5LXlvdXItZGVidA=='
58+
{%- endcodetabs %}
59+
60+
## Using the Header
61+
62+
Once the organization has been paired with a user any request that proxies through Janus will contain the `X-Organization` header with a value equal to the organization paired with the user.

entry-dev.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ echo "compile finished"
1111
if [ "$debug" == 1 ]; then
1212
dlv --listen=:40000 --headless=true --continue --accept-multiclient --api-version=2 exec ./main start
1313
else
14-
./main
14+
./main start
1515
fi

0 commit comments

Comments
 (0)