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

feature/AKD-108: Added Azure 'SQL Server Private Endpoints Configured' plugin and test cases #617

Merged
merged 5 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ module.exports = {
'advancedDataSecurityEnabled' : require(__dirname + '/plugins/azure/sqlserver/advancedDataSecurityEnabled.js'),
'tdeProtectorEncrypted' : require(__dirname + '/plugins/azure/sqlserver/tdeProtectorEncrypted.js'),
'noPublicAccess' : require(__dirname + '/plugins/azure/sqlserver/noPublicAccess.js'),
'serverPrivateEndpoints' : require(__dirname + '/plugins/azure/sqlserver/serverPrivateEndpoints.js'),
'auditRetentionPolicy' : require(__dirname + '/plugins/azure/sqlserver/auditRetentionPolicy.js'),
'auditActionGroupsEnabled' : require(__dirname + '/plugins/azure/sqlserver/auditActionGroupsEnabled.js'),
'serverAuditingEnabled' : require(__dirname + '/plugins/azure/sqlserver/serverAuditingEnabled.js'),
Expand Down
50 changes: 50 additions & 0 deletions plugins/azure/sqlserver/serverPrivateEndpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var async = require('async');
const helpers = require('../../../helpers/azure');

module.exports = {
title: 'SQL Server Private Endpoints Configured',
category: 'SQL Server',
description: 'Ensures that SQL Servers are accessible only through private endpoints',
more_info: 'Azure Private Endpoint is a network interface that connects you privately and securely to a service powered by Azure Private Link. Private Endpoint uses a private IP address from your VNet, effectively bringing the service such as Azure SQL Server into your VNet.',
recommended_action: 'Ensure that Private Endpoints are configured properly and Public Network Access is disabled for SQL Server',
link: 'https://docs.microsoft.com/en-us/azure/private-link/private-link-overview',
apis: ['servers:listSql'],

run: function(cache, settings, callback) {
var results = [];
var source = {};
var locations = helpers.locations(settings.govcloud);

async.each(locations.servers, function(location, rcb) {

var servers = helpers.addSource(cache, source,
['servers', 'listSql', location]);

if (!servers) return rcb();

if (servers.err || !servers.data) {
helpers.addResult(results, 3,
'Unable to query for SQL servers: ' + helpers.addError(servers), location);
return rcb();
}

if (!servers.data.length) {
helpers.addResult(results, 0, 'No SQL servers found', location);
return rcb();
}

for (const server of servers.data) {
if (server.privateEndpointConnections && server.privateEndpointConnections.length) {
helpers.addResult(results, 0, 'Private Endpoints are configured for the SQL Server', location, server.id);
} else {
helpers.addResult(results, 2, 'Private Endpoints are not configured for the SQL Server', location, server.id);
}
}

rcb();
}, function() {
// Global checking goes here
callback(null, results, source);
});
}
};
85 changes: 85 additions & 0 deletions plugins/azure/sqlserver/serverPrivateEndpoints.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
var expect = require('chai').expect;
var serverPrivateEndpoints = require('./serverPrivateEndpoints');

const servers = [
{
'id': '/subscriptions/123/resourceGroups/aqua-resource-group/providers/Microsoft.Sql/servers/test-server',
'name': 'test-server',
'privateEndpointConnections': [
{
'id': '/subscriptions/123/resourceGroups/aqua-resource-group/providers/Microsoft.Sql/servers/test-server/privateEndpointConnections/test-endpoint',
'provisioningState': 'Ready'
}
],
'location': 'eastus'
},
{
'id': '/subscriptions/123/resourceGroups/aqua-resource-group/providers/Microsoft.Sql/servers/test-server',
'name': 'test-server',
'privateEndpointConnections': [],
'location': 'eastus'
}
];


const createCache = (servers) => {
let server = {};
if (servers) {
server['data'] = servers;
}
return {
servers: {
listSql: {
'eastus': server
}
}
};
};

describe('serverPrivateEndpoints', function() {
describe('run', function() {
it('should give passing result if no SQL servers', function(done) {
const cache = createCache([]);
serverPrivateEndpoints.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(0);
expect(results[0].message).to.include('No SQL servers found');
expect(results[0].region).to.equal('eastus');
done();
});
});

it('should give unknown result if unable to query for SQL servers', function(done) {
const cache = createCache(null);
serverPrivateEndpoints.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(3);
expect(results[0].message).to.include('Unable to query for SQL servers:');
expect(results[0].region).to.equal('eastus');
done();
});
});

it('should give passing result if private endpoints are configured', function(done) {
const cache = createCache([servers[0]]);
serverPrivateEndpoints.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(0);
expect(results[0].message).to.include('Private Endpoints are configured for the SQL Server');
expect(results[0].region).to.equal('eastus');
done();
});
});

it('should give failing result if private endpoints are not configured', function(done) {
const cache = createCache([servers[1]]);
serverPrivateEndpoints.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(2);
expect(results[0].message).to.include('Private Endpoints are not configured for the SQL Server');
expect(results[0].region).to.equal('eastus');
done();
});
});
});
});