Skip to content

Commit

Permalink
Merge pull request #1 from ningweinw/demo
Browse files Browse the repository at this point in the history
Update for AuthN and AppInsight
  • Loading branch information
ningweinw authored Apr 18, 2020
2 parents 5a569f4 + bd43858 commit 849983c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
5 changes: 5 additions & 0 deletions public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function AppCtrl($scope, $http) {
vm.records = [];
vm.allRecords = [];
vm.pager = {};
vm.loginStr = '';

vm.handleError = function(response) {
console.log(response.status + " - " + response.statusText + " - " + response.data);
Expand All @@ -36,6 +37,10 @@ function AppCtrl($scope, $http) {
vm.getAllRecords = function() {
$http.get('/records').then(function(response){
vm.allRecords = response.data;
vm.loginStr = vm.allRecords.pop().loginStr;
if(vm.loginStr == "Anonymous")
document.getElementById("logout").style.display = "none";

console.log("Records queried from DB: " + vm.allRecords.length);
vm.setPage(1);
}, function(response){
Expand Down
11 changes: 10 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@
</head>
<body>
<div class="container" ng-controller="appCtrl as ctrl">
<h1>MEAN - Records app</h1>
<table style="width:100%">
<tr>
<td>
<h1>MEAN - Records app</h1>

</td>
<td align="right">{{ctrl.loginStr}}</td>
<td align="right" id="logout"><a href="/.auth/logout">Sign out</a></td>
</tr>
</table>
<table class="table">
<thead>
<tr>
Expand Down
34 changes: 30 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,29 @@ var express = require('express'),
ObjectId = require('mongodb').ObjectID,
redis = require("redis"),
msRestAzure = require('ms-rest-azure'),
KeyVault = require('azure-keyvault');
KeyVault = require('azure-keyvault'),
appInsights = require('applicationinsights');

const KEYVAULT_URI = null || process.env['KEYVAULT_URI'],
REDIS_HOST = null || process.env['REDIS_HOST'],
COLLECTION_NAME = 'records',
SECRET_MONGO_URL = 'MongoDB-URL',
SECRET_REDIS = 'Redis-Key';
SECRET_REDIS = 'Redis-Key',
APIM_KEY = null || process.env['APPINSIGHTS_INSTRUMENTATIONKEY'];

if(APIM_KEY) {
appInsights.setup(APIM_KEY)
.setAutoDependencyCorrelation(true)
.setAutoCollectRequests(true)
.setAutoCollectPerformance(true)
.setAutoCollectExceptions(true)
.setAutoCollectDependencies(true)
.setAutoCollectConsole(true)
.setUseDiskRetryCaching(true)
.start();
}

var loginStr = 'Anonymous';

console.log(`KEYVALUT_URI=${KEYVAULT_URI}`);
console.log(`REDIS_HOST=${REDIS_HOST}`);
Expand Down Expand Up @@ -56,7 +72,12 @@ msRestAzure.loginWithAppServiceMSI({resource: 'https://vault.azure.net'}, functi

app.get('/records', function(req, res, next) {
console.log("Received get /records request");

console.log(req.headers);
var loginName = req.header('x-ms-client-principal-name');
var idpName = req.header('x-ms-client-principal-idp');
if((loginName !== undefined) && (loginName !== null))
loginStr = loginName + ' (' + idpName + ')';

cache.HGETALL(COLLECTION_NAME, function(err, reply) {
if(err) throw err;
else if(reply) {
Expand All @@ -69,6 +90,8 @@ msRestAzure.loginWithAppServiceMSI({resource: 'https://vault.azure.net'}, functi
for(var i = 0; i < records.length; i++) {
records[i] = JSON.parse(records[i]);
}
// append the login string to the record list
records.push({"loginStr": loginStr});
res.json(records);
}
else {
Expand All @@ -82,7 +105,6 @@ msRestAzure.loginWithAppServiceMSI({resource: 'https://vault.azure.net'}, functi

console.log("Number of records from DB: " + records.length);
//console.log(records);
res.json(records);

// write to cache as hashtable, key is _id, value is the record object
var i, hashList = [];
Expand All @@ -92,6 +114,10 @@ msRestAzure.loginWithAppServiceMSI({resource: 'https://vault.azure.net'}, functi
}
cache.HMSET(COLLECTION_NAME, hashList);
console.log("Cache set successfully");

// append the login string to the record list
records.push({"loginStr": loginStr});
res.json(records);
});
}
});
Expand Down

0 comments on commit 849983c

Please sign in to comment.