Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect to API Gateway #153

Merged
merged 37 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e07dad4
WIP: Need to load app certificate in request.
kevmo May 10, 2021
9f1ba1f
Merge branch 'main' of https://github.com/cagov/ui-claim-tracker into…
kevmo May 11, 2021
36942ea
Fixing TS errors
kevmo May 11, 2021
0093c74
WIP: Console logging data.
kevmo May 14, 2021
530d7ff
App is successfully connecting to API gateway, if proper .p12 file is…
kevmo May 19, 2021
c46d872
Cleanup & improvement of codebase.
kevmo May 20, 2021
91c73a4
Code cleanup.
kevmo May 20, 2021
ef236ff
Development cruft removal.
kevmo May 20, 2021
d3a8430
Move pfx file name into an environmental variable.
kevmo May 20, 2021
781fe00
buildApiUrl function.
kevmo May 21, 2021
686c65e
remove unneeded comment
kevmo May 21, 2021
d27731b
Removing comments.
kevmo May 21, 2021
a64b2ba
TypeScript finesse.
kevmo May 21, 2021
9516c4b
Merge commit.
kevmo May 24, 2021
fcf1121
WIP
kevmo May 25, 2021
3250af2
WIP
kevmo May 26, 2021
3d58ece
TS Error resolved
kevmo May 27, 2021
457c0d4
TS Errors Resolved + agent.destroy
kevmo May 27, 2021
7ba14b4
Only load .env file when doing local development.
kevmo May 28, 2021
03a1f95
Remove console.log
kevmo May 28, 2021
ec1c2db
Merge main.
kevmo May 28, 2021
8455c6b
Stop adding in previously deleted file.
kevmo May 28, 2021
2a60679
Snapshots.
kevmo May 28, 2021
95cd590
prettier fixes and eslint bypass
kalvinwang May 28, 2021
99a160f
revert unnecessary change
kalvinwang May 28, 2021
11887b7
Commenting.
kevmo May 28, 2021
50fb37c
debug
kalvinwang May 28, 2021
a537aa0
debug
kalvinwang May 28, 2021
bed35a0
Remove certificate password.
rocketnova Jun 1, 2021
7590f65
Remove console.log statements
rocketnova Jun 2, 2021
ab674ca
Refactor env vars and remove non-null assertion.
rocketnova Jun 2, 2021
5945577
Update readme with documentation of env vars.
rocketnova Jun 2, 2021
fac89ac
Update test snapshot: slightly less whitespace.
rocketnova Jun 2, 2021
5d52672
Temporarily do some nullish coalescing.
rocketnova Jun 2, 2021
fd577ab
Remove TODOs.
rocketnova Jun 7, 2021
b0b1a48
Remove extra argument.
rocketnova Jun 7, 2021
2bd6177
Apply suggestions from code review: update comments
rocketnova Jun 7, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# secrets
/certs
kevmo marked this conversation as resolved.
Show resolved Hide resolved

# dependencies
/node_modules
/.pnp
Expand Down
8 changes: 7 additions & 1 deletion components/ClaimCard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { ClaimCardDetails } from './ClaimCardDetails'
import { ClaimCardBody } from './ClaimCardBody'

export const ClaimCard: React.FC = () => {
export interface ClaimCardProps {
uniqueNumber?: string
kevmo marked this conversation as resolved.
Show resolved Hide resolved
claimType?: string
}

export const ClaimCard: React.FC<ClaimCardProps> = ({ uniqueNumber }) => {
return (
<div className="claim-card">
<ClaimCardDetails title="Unemployment Insurance (UI)" benefitYear="3/21/2020 - 3/20/2021" claimBalance="$0.00" />
Expand All @@ -14,6 +19,7 @@ export const ClaimCard: React.FC = () => {
'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.',
]}
/>
<h1>{uniqueNumber}</h1>
kevmo marked this conversation as resolved.
Show resolved Hide resolved
</div>
)
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
},
"dependencies": {
"bootstrap": "^4.6.0",
"dotenv": "^9.0.2",
"next": "10.0.8",
"node-fetch": "^2.6.1",
"node-forge": "^0.10.0",
kevmo marked this conversation as resolved.
Show resolved Hide resolved
"pem": "^1.14.4",
"react": "17.0.1",
"react-bootstrap": "^1.5.2",
"react-dom": "17.0.1"
Expand Down
6 changes: 6 additions & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { AppProps } from 'next/app'
import { ReactElement } from 'react'
import { appWithTranslation } from 'next-i18next'

import { config } from 'dotenv'
import { resolve } from 'path'

// TODO: Make this path resolution more robust (no relative paths!)
config({path: resolve(process.cwd(), '..','.env')})
kevmo marked this conversation as resolved.
Show resolved Hide resolved

function MyApp({ Component, pageProps }: AppProps): ReactElement {
return <Component {...pageProps} />
}
Expand Down
102 changes: 95 additions & 7 deletions pages/claimstatus/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import fs from 'fs'
import path from 'path'
import https from 'https'
import { promisify } from 'util'

import Head from 'next/head'
import Container from 'react-bootstrap/Container'
import { ReactElement } from 'react'
import { useTranslation } from 'next-i18next'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { GetServerSideProps } from 'next'
import pem from 'pem'
import fetch from 'node-fetch'

import { Header } from '../../components/Header'
import { Main } from '../../components/Main'
import { Footer } from '../../components/Footer'

export default function Home(): ReactElement {

export interface Claim {
ClaimType: (string | "not working")
kevmo marked this conversation as resolved.
Show resolved Hide resolved
}

export interface HomeProps {
uniqueNumber?: string
kevmo marked this conversation as resolved.
Show resolved Hide resolved
claimData?: Claim[]
}

export default function Home({uniqueNumber, claimData}: HomeProps): ReactElement {
const { t } = useTranslation('common')

return (
Expand All @@ -18,16 +35,87 @@ export default function Home(): ReactElement {
<title>{t('title')}</title>
<link rel="icon" href="/favicon.ico" />
</Head>

<Header />
<Main />
<Footer />
{console.log("-Test Response-")} {/* testing, to be removed before launch */}
kevmo marked this conversation as resolved.
Show resolved Hide resolved
{console.dir({claimData})} {/* testing */}
kevmo marked this conversation as resolved.
Show resolved Hide resolved
</Container>
)
}

export const getServerSideProps: GetServerSideProps = async ({ locale }) => ({
props: {
...(await serverSideTranslations(locale || 'en', ['common', 'header', 'footer'])),
},
})
export const getServerSideProps: GetServerSideProps = async ({ req, locale }) => {
const PASSWORD = process.env.CERTIFICATE_PASSPHRASE
const API_URL: string = process.env.API_URL || "fail gracefully" //TODO: Fail gracefully.
kevmo marked this conversation as resolved.
Show resolved Hide resolved
// const UNIQUE_NUMBER_HEADER: string = process.env.UNIQUE_NUMBER_HEADER_TITLE || "x-unique-number"
const CERT_DIR: (string) = (process.env.CERTIFICATE_DIR || "/var/ssl")
kevmo marked this conversation as resolved.
Show resolved Hide resolved
const KEY_PATH: string = path.join(CERT_DIR, '/ct.p12')
kevmo marked this conversation as resolved.
Show resolved Hide resolved

let apiData: (string | null) = null

// return certificate object with cert and key fields.
async function getCertificate() {
const pemReadPkcs12 = promisify(pem.readPkcs12)
const pfx = fs.readFileSync(KEY_PATH);

const cert = await pemReadPkcs12(pfx, { p12Password: PASSWORD })

return cert;
}

// TODO: Acquire uniqueNumber from header and use it to build API string
kevmo marked this conversation as resolved.
Show resolved Hide resolved
// that will eventually be passed to makeRequest.
// TODO: if no uniqueNumber, redirect.
kevmo marked this conversation as resolved.
Show resolved Hide resolved
// const requestHeaders: IncomingHttpHeaders = req.headers;
// const uniqueNumber = requestHeaders[UNIQUE_NUMBER_HEADER];
// function buildApiUrl(uniqueNumber, USER_KEY){}

async function makeRequest(certificate) {
const headers = {
Accept: 'application/json'
};

/* https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options

// TODO: Store certs in memory, instead of loading every time.
kevmo marked this conversation as resolved.
Show resolved Hide resolved
// TODO: rejectUnauthorized - set to true or implement a `checkServerIdentity`
kevmo marked this conversation as resolved.
Show resolved Hide resolved
// function to check that the certificate is actually
// issued by the host you're connecting to.
// https://nodejs.org/api/https.html#https_https_request_url_options_callback
*/
const options = {
cert: certificate.cert,
key: certificate.key,
rejectUnauthorized: false,
keepAlive: false
};
const sslConfiguredAgent = new https.Agent(options);

try {
const response = await fetch(API_URL, {
kevmo marked this conversation as resolved.
Show resolved Hide resolved
headers: headers,
agent: sslConfiguredAgent,
});

const responseBody = await response.json();

apiData = responseBody

} catch (error) {
console.log(error);
}

return apiData;
}

// where it comes together
const certificate = await getCertificate()
const data = await makeRequest(certificate)

return ({
props: {
claimData: [data],
...(await serverSideTranslations(locale || 'en', ['common', 'header', 'footer'])),
kevmo marked this conversation as resolved.
Show resolved Hide resolved
}
})
}
48 changes: 46 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4901,6 +4901,11 @@ chardet@^0.7.0:
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
integrity sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==

charenc@0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667"
integrity sha1-wKHS86cJLgN3S/qD8UwPxXkKhmc=

chokidar@3.5.1, "chokidar@>=2.0.0 <4.0.0", chokidar@^3.4.1, chokidar@^3.4.2:
version "3.5.1"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.1.tgz#ee9ce7bbebd2b79f49f304799d5468e31e14e68a"
Expand Down Expand Up @@ -5463,6 +5468,11 @@ cross-spawn@^6.0.0, cross-spawn@^6.0.5:
shebang-command "^1.2.0"
which "^1.2.9"

crypt@0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b"
integrity sha1-iNf/fsDfuG9xPch7u0LQRNPmxBs=

crypto-browserify@3.12.0, crypto-browserify@^3.11.0:
version "3.12.0"
resolved "https://registry.yarnpkg.com/crypto-browserify/-/crypto-browserify-3.12.0.tgz#396cf9f3137f03e4b8e532c58f698254e00f80ec"
Expand Down Expand Up @@ -5980,6 +5990,11 @@ dotenv@^8.0.0, dotenv@^8.2.0:
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==

dotenv@^9.0.2:
version "9.0.2"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-9.0.2.tgz#dacc20160935a37dea6364aa1bef819fb9b6ab05"
integrity sha512-I9OvvrHp4pIARv4+x9iuewrWycX6CcZtoAu1XrzPxc5UygMJXJZYmBsynku8IkrJwgypE5DGNjDPmPRhDCptUg==

downshift@^6.0.15:
version "6.1.2"
resolved "https://registry.yarnpkg.com/downshift/-/downshift-6.1.2.tgz#99d9a03d4da4bf369df766effc3b70f7e789950e"
Expand Down Expand Up @@ -6228,6 +6243,11 @@ es5-shim@^4.5.13:
resolved "https://registry.yarnpkg.com/es5-shim/-/es5-shim-4.5.15.tgz#6a26869b261854a3b045273f5583c52d390217fe"
integrity sha512-FYpuxEjMeDvU4rulKqFdukQyZSTpzhg4ScQHrAosrlVpR6GFyaw14f74yn2+4BugniIS0Frpg7TvwZocU4ZMTw==

es6-promisify@^6.0.0:
version "6.1.1"
resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-6.1.1.tgz#46837651b7b06bf6fff893d03f29393668d01621"
integrity sha512-HBL8I3mIki5C1Cc9QjKUenHtnG0A5/xA8Q/AllRcfiwl2CZFXGK7ddBiCoRwAix4i2KxcQfjtIVcrVbB3vbmwg==

es6-shim@^0.35.5:
version "0.35.6"
resolved "https://registry.yarnpkg.com/es6-shim/-/es6-shim-0.35.6.tgz#d10578301a83af2de58b9eadb7c2c9945f7388a0"
Expand Down Expand Up @@ -8119,7 +8139,7 @@ is-boolean-object@^1.1.0:
dependencies:
call-bind "^1.0.0"

is-buffer@^1.1.5:
is-buffer@^1.1.5, is-buffer@~1.1.6:
version "1.1.6"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==
Expand Down Expand Up @@ -9628,6 +9648,15 @@ md5.js@^1.3.4:
inherits "^2.0.1"
safe-buffer "^5.1.2"

md5@^2.2.1:
version "2.3.0"
resolved "https://registry.yarnpkg.com/md5/-/md5-2.3.0.tgz#c3da9a6aae3a30b46b7b0c349b87b110dc3bda4f"
integrity sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==
dependencies:
charenc "0.0.2"
crypt "0.0.2"
is-buffer "~1.1.6"

mdast-squeeze-paragraphs@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/mdast-squeeze-paragraphs/-/mdast-squeeze-paragraphs-4.0.0.tgz#7c4c114679c3bee27ef10b58e2e015be79f1ef97"
Expand Down Expand Up @@ -10119,6 +10148,11 @@ node-fetch@2.6.1, node-fetch@^2.6.0, node-fetch@^2.6.1:
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==

node-forge@^0.10.0:
version "0.10.0"
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.10.0.tgz#32dea2afb3e9926f02ee5ce8794902691a676bf3"
integrity sha512-PPmu8eEeG9saEUvI97fm4OYxXVB6bFvyNTyiUOBichBpFG8A1Ljw3bY62+5oOjDEMHRnd0Y7HQ+x7uzxOzC6JA==

node-html-parser@1.4.9:
version "1.4.9"
resolved "https://registry.yarnpkg.com/node-html-parser/-/node-html-parser-1.4.9.tgz#3c8f6cac46479fae5800725edb532e9ae8fd816c"
Expand Down Expand Up @@ -10451,7 +10485,7 @@ os-browserify@^0.3.0:
resolved "https://registry.yarnpkg.com/os-browserify/-/os-browserify-0.3.0.tgz#854373c7f5c2315914fc9bfc6bd8238fdda1ec27"
integrity sha1-hUNzx/XCMVkU/Jv8a9gjj92h7Cc=

os-tmpdir@~1.0.2:
os-tmpdir@^1.0.1, os-tmpdir@~1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274"
integrity sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=
Expand Down Expand Up @@ -10764,6 +10798,16 @@ pbkdf2@^3.0.3:
safe-buffer "^5.0.1"
sha.js "^2.4.8"

pem@^1.14.4:
version "1.14.4"
resolved "https://registry.yarnpkg.com/pem/-/pem-1.14.4.tgz#a68c70c6e751ccc5b3b5bcd7af78b0aec1177ff9"
integrity sha512-v8lH3NpirgiEmbOqhx0vwQTxwi0ExsiWBGYh0jYNq7K6mQuO4gI6UEFlr6fLAdv9TPXRt6GqiwE37puQdIDS8g==
dependencies:
es6-promisify "^6.0.0"
md5 "^2.2.1"
os-tmpdir "^1.0.1"
which "^2.0.2"

pend@~1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/pend/-/pend-1.2.0.tgz#7a57eb550a6783f9115331fcf4663d5c8e007a50"
Expand Down