aboutsummaryrefslogtreecommitdiff
path: root/templates/index.tpl
blob: 2a5e85346a42c2270c78fb262f0b83ff6ef24c4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Select Account</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<style type="text/css">
            body {
                margin: 0 10%;
            }
			table {
                width: 100%;
				border: 1px solid black;
				border-collapse: collapse;
			}
			td, th {
				border: 1px solid black;
				padding: 0.5em;
			}
			th {
				background-color: #CCCCCC;
			}
			td.alt-creds {
				text-align: center;
			}
            textarea {
                height: 10em;
                width: 100%;
            }
            a, a:visited {
                color: blue;
            }
            h1 {
                text-align: center;
            }
            tt {
                background-color: #CCC;
                border: 1px solid #999;
                padding: 0.25em;
            }
            #api-key {
                margin: auto;
            }
            #api-key textarea {
                height: 100%;
            }
            #api-error {
                border: 0.25em solid red;
                color: white;
                font-size: 2em;
                background-color: #ff8080;
                padding: 1em;
                text-align: center;
                display: none;
            }
		</style>
		<script id="shell_template" type="text/template">
			export AWS_CREDS_EXPIRATION="[[ .expiration ]]"
			export AWS_ACCESS_KEY_ID="[[ .access_key ]]"
			export AWS_SECRET_ACCESS_KEY="[[ .secret_key ]]"
			export AWS_SESSION_TOKEN="[[ .session_token ]]"
		</script>
		<script id="powershell_template" type="text/template">
			Set-Item -path env:AWS_CREDS_EXPIRATION -value '[[ .expiration ]]'
			Set-Item -path env:AWS_ACCESS_KEY_ID -value '[[ .access_key ]]'
			Set-Item -path env:AWS_SECRET_ACCESS_KEY -value '[[ .secret_key ]]'
			Set-Item -path env:AWS_SESSION_TOKEN -value '[[ .session_token ]]'
		</script>
		<script id="aws_config_template" type="text/template">
			[profile [[ .ShortName ]]]
			aws_access_key_id=[[ .access_key ]]
			aws_secret_access_key=[[ .secret_key ]]
			aws_session_token=[[ .session_token ]]
			expiration=[[ .expiration ]]
		</script>
		<script id="credential_row_template" type="text/template">
            <body>
            <tr id="credentials-for-[[ .Account ]]">
                <td colspan="2">
                    <textarea>[[ .Content ]]</textarea>
                    <button>Close</button>
                </td>
            </tr>
            </body>
        </script>
        <script id="account_row_template" type="text/template">
            <tr id="account-row-[[ .short_name ]]" data-account-name="[[ .short_name ]]" data-global-credential-endpoint="[[ .global_credential_url ]]">
				<td>[[ .name ]]</td>
				<td class="alt-creds">
                    <a href="[[ .console_redirect_url ]]">Console</a> |
                    <a data-template="aws_config_template" href="#/cli/[[ .short_name ]]">AWS CLI</a> |
					<a data-template="shell_template" href="#/sh/[[ .short_name ]]">Bash</a> |
					<a data-template="powershell_template" href="#/ps/[[ .short_name ]]">Powershell</a>
				</td>
			</tr>
        </script>
        <script type="text/javascript">
            function fillTemplate(templateId, values) {
                var tpl = document.getElementById(templateId).text;

                Object.keys(values).forEach(function(key) {
                    tpl = tpl.replace(new RegExp("\\[\\[ \\." + key + " \\]\\]", "g"), values[key]);
                });

                var out = [];
                tpl.split("\n").forEach(function(line) {
                    line = line.replace(/^\s+/, "").replace(/\s+$/, "");
                    if (line !== "") {
                        out.push(line);
                    }
                });

                return out.join("\n");
            }

            function getJSON(response) {
                if (!response.ok) {
                    document.getElementById("api-error").style.display = "block";
                    throw new Error("Error loading api");
                }

                return response.json();
            }

            function accountTableLinkClick(event) {
                event.preventDefault();

                var thisRow = event.target.parentElement.parentElement;
                var template = event.target.getAttribute("data-template");
                var account = thisRow.getAttribute("data-account-name");
                var credentialEndpoint = thisRow.getAttribute("data-global-credential-endpoint");
                var oldText = event.target.text;

                var existingTr = document.getElementById("credentials-for-" + account);
                if (existingTr) {
                    existingTr.remove();
                }

                event.target.text = "Loading...";

                fetch(credentialEndpoint).then(getJSON).then(function(vals) {
                    vals["ShortName"] = account;

                    var newTr = fillTemplate("credential_row_template", {
                        "Account": account,
                        "Content": fillTemplate(template, vals)
                    });

                    event.target.text = oldText;
                    thisRow.insertAdjacentHTML("afterend", newTr);
                    thisRow.nextElementSibling.getElementsByTagName("button")[0].addEventListener("click", function(event) {
                        event.target.parentNode.parentNode.remove();
                    });
                });

                return false;
            }

            function populateAccountTable() {
                fetch("/api/account").then(getJSON).then(function(response) {
                    response.forEach(populateAccountRow);
                });
            }

            function populateAccountRow(row) {
                var out = fillTemplate("account_row_template", row);
                document.querySelector("#account-table tr").insertAdjacentHTML("afterend", out);

                document.querySelectorAll("#account-row-" + row["short_name"] + " a[data-template]").forEach(function(element) {
                    element.addEventListener("click", accountTableLinkClick);
                });
            }

            function getCookie(name) {
                return document.cookie.match(new RegExp(name + "=\"?([^;\"]*)\"?;?"))[1];
            }

            function parseJWTExpires(token) {
                return new Date(JSON.parse(atob(token.split(".")[1]))["exp"] * 1000);
            }

            function populateAPIKey() {
                document.querySelector("#api-key textarea").innerText = "Bearer " + getCookie("github-token");
                document.querySelector("#session-expires").innerText = parseJWTExpires(getCookie("github-token"));
            }

            window.addEventListener('load', populateAPIKey);
            window.addEventListener('load', populateAccountTable);
        </script>
	</head>
	<body>
        <p id="api-error">Error communicating with the API.</p>

        <h1>AWS Accounts</h1>
		<table id="account-table">
			<tr>
				<th>Console Login</th>
				<th>Get Credentials</th>
			</tr>
		</table>

        <h1>API Key</h1>
        <div id="api-key">
            <p>To access <a href="/api/account">the API</a> set the value of <tt>Authorization</tt> header to:</p>
            <textarea>Bearer {token}</textarea>
            <p><b>Token Expires:</b> <span id="session-expires"></span></p>
        </div>
	</body>
</html>