Skip to content

Commit

Permalink
Merge pull request #1736 from fatima99s/postgresqlPrivateAccess
Browse files Browse the repository at this point in the history
privateAccessEnabled
  • Loading branch information
mehakseedat63 authored Dec 5, 2023
2 parents ba51bb3 + b71f99b commit ac71cb6
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
1 change: 1 addition & 0 deletions exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ module.exports = {
'geoRedundantBackupEnabled' : require(__dirname + '/plugins/azure/postgresqlserver/geoRedundantBackupEnabled.js'),
'postgresqlServerHasTags' : require(__dirname + '/plugins/azure/postgresqlserver/postgresqlServerHasTags.js'),
'postgresqlInfraDoubleEncryption': require(__dirname + '/plugins/azure/postgresqlserver/postgresqlInfraDoubleEncryption.js'),
'postgresqlPrivateEndpoints' : require(__dirname + '/plugins/azure/postgresqlserver/postgresqlPrivateEndpoints.js'),
'azureServicesAccessDisabled' : require(__dirname + '/plugins/azure/postgresqlserver/azureServicesAccessDisabled.js'),
'diagnosticLoggingEnabled' : require(__dirname + '/plugins/azure/postgresqlserver/diagnosticLoggingEnabled.js'),
'flexibleServerSCRAMEnabled' : require(__dirname + '/plugins/azure/postgresqlserver/flexibleServerSCRAMEnabled.js'),
Expand Down
50 changes: 50 additions & 0 deletions plugins/azure/postgresqlserver/postgresqlPrivateEndpoints.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: 'PostgreSQL Server Private Endpoints Configured',
category: 'PostgreSQL Server',
domain: 'Databases',
description: 'Ensures that PostgreSQL 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 PostgreSQL Server',
link: 'https://learn.microsoft.com/en-us/azure/private-link/private-link-overview',
apis: ['servers:listPostgres'],

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', 'listPostgres', location]);

if (!servers) return rcb();

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

if (!servers.data.length) {
helpers.addResult(results, 0, 'No PostgreSQL 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 PostgreSQL Server', location, server.id);
} else {
helpers.addResult(results, 2, 'Private Endpoints are not configured for the PostgreSQL Server', location, server.id);
}
}

rcb();
}, function() {
callback(null, results, source);
});
}
};
136 changes: 136 additions & 0 deletions plugins/azure/postgresqlserver/postgresqlPrivateEndpoints.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
var expect = require('chai').expect;
var privateEndpoints = require('./postgresqlPrivateEndpoints');

const listPostgres = [
{
'sku': {
'name': 'B_Gen5_1',
'tier': 'Basic',
'family': 'Gen5',
'capacity': 1
},
'location': 'eastus',
'tags': { "key": "value" },
'id': '/subscriptions/123/resourceGroups/aqua-resource-group/providers/Microsoft.DBforPostgreSQL/servers/server1',
'name': 'server1',
'type': 'Microsoft.DBforPostgreSQL/servers',
'administratorLogin': 'test',
'storageProfile': {
'storageMB': 5120,
'backupRetentionDays': 7,
'geoRedundantBackup': 'Disabled',
'storageAutogrow': 'Enabled'
},
'version': '11',
'sslEnforcement': 'Enabled',
'minimalTlsVersion': 'TLS1_0',
'userVisibleState': 'Ready',
'fullyQualifiedDomainName': 'server1.postgres.database.azure.com',
'earliestRestoreDate': '2021-03-10T12:45:13.233+00:00',
'replicationRole': '',
'masterServerId': '',
'byokEnforcement': 'Disabled',
'privateEndpointConnections': [],
'infrastructureEncryption': 'Disabled',
'publicNetworkAccess': 'Enabled'
},
{
'sku': {
'name': 'B_Gen5_1',
'tier': 'Basic',
'family': 'Gen5',
'capacity': 1
},
'location': 'eastus',
'tags': { "key": "value" },
'id': '/subscriptions/123/resourceGroups/aqua-resource-group/providers/Microsoft.DBforPostgreSQL/servers/server1',
'name': 'server1',
'type': 'Microsoft.DBforPostgreSQL/servers',
'administratorLogin': 'test',
'storageProfile': {
'storageMB': 5120,
'backupRetentionDays': 7,
'geoRedundantBackup': 'Disabled',
'storageAutogrow': 'Enabled'
},
'version': '11',
'sslEnforcement': 'Enabled',
'minimalTlsVersion': 'TLS1_2',
'userVisibleState': 'Ready',
'fullyQualifiedDomainName': 'server1.postgres.database.azure.com',
'earliestRestoreDate': '2021-03-10T12:45:13.233+00:00',
'replicationRole': '',
'masterServerId': '',
'byokEnforcement': 'Disabled',
'privateEndpointConnections': [
{
'id': '/subscriptions/123/resourceGroups/aqua-resource-group/providers/Microsoft.Sql/servers/test-server/privateEndpointConnections/test-endpoint',
'provisioningState': 'Ready'
}
],
'infrastructureEncryption': 'Disabled',
'publicNetworkAccess': 'Enabled'
}

];

const createCache = (listPostgres) => {
return {
servers: {
listPostgres: {
'eastus': {
data: listPostgres
}
}
}
};
};

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

it('should give failing result if private endpoints are not configured', function(done) {
const cache = createCache([listPostgres[0]]);
privateEndpoints.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 PostgreSQL Server');
expect(results[0].region).to.equal('eastus');
done();
});
});

it('should give should give passing result if private endpoints are configured', function(done) {
const cache = createCache([listPostgres[1]]);
privateEndpoints.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 PostgreSQL Server');
expect(results[0].region).to.equal('eastus');
done();
});
});

it('should give UnKnown result if unable to query postgreSQL Server', function(done) {
const cache = createCache(null);
privateEndpoints.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 PostgreSQL servers: ');
expect(results[0].region).to.equal('eastus');
done();
});
});

})
})

0 comments on commit ac71cb6

Please sign in to comment.