Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 73cbb62

Browse files
authored
Merge pull request #86 from deckgo/nm-login
Add user login
2 parents d71808a + 66c9dcf commit 73cbb62

File tree

28 files changed

+1213
-185
lines changed

28 files changed

+1213
-185
lines changed

.circleci/config.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
version: 2
2+
3+
jobs:
4+
build:
5+
working_directory: ~/project/infra
6+
machine:
7+
enabled: true
8+
steps:
9+
- checkout:
10+
path: ~/project
11+
12+
- run:
13+
name: Install Nix
14+
command: |
15+
sudo mkdir -p /nix
16+
sudo chown circleci /nix
17+
bash <(curl https://nixos.org/nix/install)
18+
echo '. /home/circleci/.nix-profile/etc/profile.d/nix.sh' >> $BASH_ENV
19+
sudo mkdir -p /etc/nix
20+
21+
# Enable sandbox
22+
echo "build-use-sandbox = true" | sudo tee -a /etc/nix/nix.conf
23+
echo "substituters = https://cache.nixos.org https://static-haskell-nix.cachix.org https://deckgo.cachix.org" \
24+
| sudo tee -a /etc/nix/nix.conf
25+
echo "trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= static-haskell-nix.cachix.org-1:Q17HawmAwaM1/BfIxaEDKAxwTOyRVhPG5Ji9K3+FvUU= deckgo.cachix.org-1:Kx6Rm054j44GugSRodI2R8T7tAr2u63gKbcCQ9wgaUk=" \
26+
| sudo tee -a /etc/nix/nix.conf
27+
28+
- run:
29+
name: Install cachix
30+
command: |
31+
nix-env -iA cachix -f https://cachix.org/api/v1/install
32+
33+
- run:
34+
name: Run cachix
35+
command: |
36+
cachix push deckgo --watch-store
37+
background: true
38+
39+
- run:
40+
name: Nix build
41+
command: |
42+
./script/test
43+
44+
- run:
45+
name: "Update Node.js and npm"
46+
command: |
47+
nix-env -f ./nix -iA nodejs-10_x
48+
49+
- run:
50+
name: Install netlify-cli
51+
command: |
52+
npm install netlify-cli
53+
54+
- run: # TODO: shouldn't deploy to prod on every commit
55+
name: Netlify deploy
56+
command: |
57+
echo "Branch:" "$CIRCLE_BRANCH"
58+
echo "Repo:" "$CIRCLE_REPOSITORY_URL"
59+
echo "PR:" "$CIRCLE_PULL_REQUEST"
60+
if [ "$CIRCLE_BRANCH" == "master" ]; then
61+
echo "Deploying to production"
62+
./node_modules/netlify-cli/bin/run deploy \
63+
--dir=$(nix-build -A swaggerUi --no-link) \
64+
--message="$CIRCLE_SHA1" --prod
65+
elif [ -n "$CIRCLE_PULL_REQUEST" ]; then
66+
echo "One time deploy for PR $CIRCLE_PR_NUMBER"
67+
./node_modules/netlify-cli/bin/run deploy \
68+
--dir=$(nix-build -A swaggerUi --no-link) \
69+
--message="$CIRCLE_SHA1"
70+
else
71+
echo "Not deploying"
72+
fi
73+
74+
workflows:
75+
version: 2
76+
build:
77+
jobs:
78+
- build:
79+
context: cachix

infra/default.nix

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,96 @@ rec
1414
tar -xvf ${pkgs.sources.dynamodb}
1515
'';
1616

17-
test = pkgs.runCommand "tests" { buildInputs = [ pkgs.jre pkgs.curl pkgs.netcat pkgs.awscli ]; }
17+
publicKey = builtins.readFile ./public.cer;
18+
19+
swaggerUi = pkgs.runCommand "swagger-ui" {}
20+
''
21+
mkdir -p $out
22+
${handler}/bin/swagger $out
23+
'';
24+
25+
googleResp = { "key1" = publicKey ; };
26+
27+
apiDir = pkgs.writeTextFile
28+
{ name = "google-resp";
29+
destination = "/robot/v1/metadata/x509/[email protected]";
30+
text = builtins.toJSON googleResp;
31+
};
32+
33+
# TODO: don't use latest dynamodb (but pin version)
34+
35+
test = pkgs.runCommand "tests"
36+
{ buildInputs =
37+
[ pkgs.jre
38+
pkgs.netcat
39+
pkgs.awscli
40+
pkgs.haskellPackages.wai-app-static
41+
];
42+
}
1843
''
1944
20-
java -Djava.library.path=${dynamoJar}/DynamoDBLocal_lib -jar ${dynamoJar}/DynamoDBLocal.jar -sharedDb -port 8000 &
45+
# Set up DynamoDB
46+
java \
47+
-Djava.library.path=${dynamoJar}/DynamoDBLocal_lib \
48+
-jar ${dynamoJar}/DynamoDBLocal.jar \
49+
-sharedDb -port 8000 &
2150
2251
while ! nc -z 127.0.0.1 8000; do
2352
echo waiting for DynamoDB
2453
sleep 1
2554
done
55+
2656
export AWS_DEFAULT_REGION=us-east-1
2757
export AWS_ACCESS_KEY_ID=dummy
2858
export AWS_SECRET_ACCESS_KEY=dummy
2959
60+
aws dynamodb create-table \
61+
--table-name Users \
62+
--attribute-definitions \
63+
AttributeName=UserId,AttributeType=S \
64+
--key-schema AttributeName=UserId,KeyType=HASH \
65+
--endpoint-url http://127.0.0.1:8000 \
66+
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
67+
> /dev/null
68+
3069
aws dynamodb create-table \
3170
--table-name Decks \
3271
--attribute-definitions \
3372
AttributeName=DeckId,AttributeType=S \
3473
--key-schema AttributeName=DeckId,KeyType=HASH \
3574
--endpoint-url http://127.0.0.1:8000 \
36-
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
75+
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
76+
> /dev/null
3777
3878
aws dynamodb create-table \
3979
--table-name Slides \
4080
--attribute-definitions \
4181
AttributeName=SlideId,AttributeType=S \
4282
--key-schema AttributeName=SlideId,KeyType=HASH \
4383
--endpoint-url http://127.0.0.1:8000 \
44-
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
84+
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
85+
> /dev/null
4586
87+
# Start server with fs redirect for getProtocolByName
4688
NIX_REDIRECTS=/etc/protocols=${pkgs.iana-etc}/etc/protocols \
4789
LD_PRELOAD="${pkgs.libredirect}/lib/libredirect.so" \
4890
${handler}/bin/server &
4991
5092
while ! nc -z 127.0.0.1 8080; do
93+
echo waiting for server
94+
sleep 1
95+
done
96+
97+
# Set up mock server for Google public keys
98+
cp ${pkgs.writeText "google-x509" (builtins.toJSON googleResp)} cert
99+
warp -d ${apiDir} -p 8081 &
100+
while ! nc -z 127.0.0.1 8081; do
51101
echo waiting for warp
52102
sleep 1
53103
done
54104
55105
echo "Running tests"
56-
${handler}/bin/test
106+
${handler}/bin/test ${./token}
57107
58108
touch $out
59109
'';

infra/dynamo.tf

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
resource "aws_dynamodb_table" "deckdeckgo-test-dynamodb-table" {
1+
resource "aws_dynamodb_table" "deckdeckgo-test-dynamodb-table-users" {
2+
name = "Users"
3+
billing_mode = "PAY_PER_REQUEST"
4+
hash_key = "UserId"
5+
6+
attribute {
7+
name = "UserId"
8+
type = "S"
9+
}
10+
11+
}
12+
13+
resource "aws_dynamodb_table" "deckdeckgo-test-dynamodb-table-decks" {
214
name = "Decks"
315
billing_mode = "PAY_PER_REQUEST"
416
hash_key = "DeckId"

infra/firebase-login/default.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# TODO: port tests
2+
# TODO: fix sources
3+
# TODO: drop nix/packages
4+
with { pkgs = import ./nix {}; };
5+
pkgs.callPackage ./nix/packages.nix {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{ sources ? import ./sources.nix }:
2+
with
3+
{ overlay = _: pkgs: rec
4+
{ inherit (import sources.niv {}) niv;
5+
haskellPackages = pkgs.haskellPackages.override
6+
{ overrides = _: super:
7+
{ jose = super.callCabal2nix "jose" sources.hs-jose {}; };
8+
};
9+
10+
packages = import ./packages.nix
11+
{ inherit (pkgs) haskell lib ;
12+
inherit haskellPackages;
13+
};
14+
};
15+
};
16+
import sources.nixpkgs
17+
{ overlays = [ overlay ] ; config = {}; }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{ haskell
2+
, haskellPackages
3+
, lib
4+
, runCommand
5+
, writeText
6+
, zip
7+
}:
8+
rec
9+
{ firebase-login-sdist = haskell.lib.sdistTarball firebase-login;
10+
firebase-login = haskellPackages.callCabal2nix "firebase-login" firebase-login-source {};
11+
firebase-login-source = lib.sourceByRegex ../.
12+
[ "^package.yaml$"
13+
"^src.*"
14+
"^examples.*"
15+
"^README.md$"
16+
"^LICENSE$"
17+
];
18+
firebase-login-version-file = writeText "version" firebase-login.version;
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"nixpkgs": {
3+
"url": "https://github.com/NixOS/nixpkgs-channels/archive/395a543f3605ea7c17797ad33fda0c251b802978.tar.gz",
4+
"owner": "NixOS",
5+
"branch": "nixos-18.09",
6+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
7+
"repo": "nixpkgs-channels",
8+
"type": "tarball",
9+
"sha256": "0az7333nr9fax6885kj7s61c0hs6wblj7a2y78k4pq0jnhjxqzzg",
10+
"description": "Nixpkgs/NixOS branches that track the Nixpkgs/NixOS channels",
11+
"rev": "395a543f3605ea7c17797ad33fda0c251b802978"
12+
},
13+
"hs-jose": {
14+
"homepage": "http://hackage.haskell.org/package/jose",
15+
"url": "https://github.com/frasertweedale/hs-jose/archive/71274bf64c0600c1d877152173a08a5bff7adf4d.tar.gz",
16+
"owner": "frasertweedale",
17+
"branch": "master",
18+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
19+
"repo": "hs-jose",
20+
"type": "tarball",
21+
"sha256": "0ah189vika1s0jk8f17mn77gilkw24vbs6xlggxw1qj926i6c4pk",
22+
"description": "Haskell JOSE and JWT library",
23+
"rev": "71274bf64c0600c1d877152173a08a5bff7adf4d"
24+
},
25+
"niv": {
26+
"homepage": "https://github.com/nmattia/niv",
27+
"url": "https://github.com/nmattia/niv/archive/f57c85d05e6c2dd359f901d936f896e4f117d3e6.tar.gz",
28+
"owner": "nmattia",
29+
"branch": "master",
30+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
31+
"repo": "niv",
32+
"type": "tarball",
33+
"sha256": "0fbmbc73qgd4f07pag18zkdh65wxv406jm3rdrrfkk85l1inscg3",
34+
"description": "Easy dependency management for Nix projects",
35+
"rev": "f57c85d05e6c2dd359f901d936f896e4f117d3e6"
36+
}
37+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# A record, from name to path, of the third-party packages
2+
with
3+
{
4+
sources = builtins.fromJSON (builtins.readFile ./sources.json);
5+
6+
# fetchTarball version that is compatible between all the sources of Nix
7+
fetchTarball =
8+
{ url, sha256 }:
9+
if builtins.lessThan builtins.nixVersion "1.12" then
10+
builtins.fetchTarball { inherit url; }
11+
else
12+
builtins.fetchTarball { inherit url sha256; };
13+
mapAttrs = builtins.mapAttrs or
14+
(f: set: with builtins;
15+
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)));
16+
};
17+
18+
# NOTE: spec must _not_ have an "outPath" attribute
19+
mapAttrs (_: spec:
20+
if builtins.hasAttr "outPath" spec
21+
then abort
22+
"The values in sources.json should not have an 'outPath' attribute"
23+
else
24+
if builtins.hasAttr "url" spec && builtins.hasAttr "sha256" spec
25+
then
26+
spec //
27+
{ outPath = fetchTarball { inherit (spec) url sha256; } ; }
28+
else spec
29+
) sources

infra/firebase-login/package.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: firebase-login
2+
maintainer: Nicolas Mattia <[email protected]>
3+
copyright: (c) 2019 David Dal Busco and Nicolas Mattia
4+
license: MIT
5+
6+
dependencies:
7+
- aeson
8+
- base
9+
- bytestring
10+
- http-client
11+
- http-client-tls
12+
- http-conduit
13+
- jose >= 0.8.0.0 # For fromX509Certificate
14+
- lens
15+
- mtl
16+
- network-uri
17+
- pem
18+
- servant
19+
- servant-client-core
20+
- servant-server
21+
- servant-swagger
22+
- text
23+
- unordered-containers
24+
- wai
25+
- word8
26+
- x509
27+
28+
ghc-options:
29+
- -Wall
30+
31+
library:
32+
source-dirs: src

infra/firebase-login/script/test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
# vim: filetype=sh
3+
4+
nix-build --no-link

0 commit comments

Comments
 (0)