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-152: Added Google PostreSQL Log Checkpoints plugin and test cases #653

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 @@ -650,6 +650,7 @@ module.exports = {
'dbPubliclyAccessible' : require(__dirname + '/plugins/google/sql/dbPubliclyAccessible.js'),
'dbSSLEnabled' : require(__dirname + '/plugins/google/sql/dbSSLEnabled.js'),
'anyHostRootAccess' : require(__dirname + '/plugins/google/sql/anyHostRootAccess.js'),
'postgresqlLogCheckpoints' : require(__dirname + '/plugins/google/sql/postgresqlLogCheckpoints.js'),

'bucketVersioning' : require(__dirname + '/plugins/google/storage/bucketVersioning.js'),
'bucketLogging' : require(__dirname + '/plugins/google/storage/bucketLogging.js'),
Expand Down
66 changes: 66 additions & 0 deletions plugins/google/sql/postgresqlLogCheckpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var async = require('async');
var helpers = require('../../../helpers/google');

module.exports = {
title: 'PostgreSQL Log Checkpoints Enabled',
category: 'SQL',
description: 'Ensure that log_checkpoints flag is enabled for PostgreSQL instances.',
more_info: 'When log_checkpoints flag is enabled, instance checkpoints and restart points are logged in the server log.',
link: 'https://cloud.google.com/sql/docs/postgres/flags#setting_a_database_flag',
recommended_action: 'Ensure that all PostgreSQL database instances have log_checkpoints flag and it value is set to on.',
apis: ['instances:sql:list'],

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

async.each(regions.instances.sql, function(region, rcb){
let sqlInstances = helpers.addSource(
cache, source, ['instances', 'sql', 'list', region]);

if (!sqlInstances) return rcb();

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

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

sqlInstances.data.forEach(sqlInstance => {
if (sqlInstance.databaseVersion && !sqlInstance.databaseVersion.toUpperCase().startsWith('POSTGRES')) {
helpers.addResult(results, 0, 'SQL instance database version is not of PosgreSQL type', region, sqlInstance.name);
return;
}

if (sqlInstance.instanceType != "READ_REPLICA_INSTANCE" &&
sqlInstance.settings &&
sqlInstance.settings.databaseFlags) {
let found = sqlInstance.settings.databaseFlags.find(flag => flag.name && flag.name == 'log_checkpoints' &&
flag.value && flag.value == 'on');

if (found) {
helpers.addResult(results, 0,
'PostgreSQL instance has log_checkpoints flag enabled', region, sqlInstance.name);
} else {
helpers.addResult(results, 2,
'PostgreSQL instance does not have log_checkpoints flag enabled', region, sqlInstance.name);
}
} else if (sqlInstance.instanceType == "READ_REPLICA_INSTANCE") {
} else {
helpers.addResult(results, 2,
'PostgreSQL instance does not have log_checkpoints flag enabled', region, sqlInstance.name);
}
});

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

const createCache = (err, data) => {
return {
instances: {
sql: {
list: {
'global': {
err: err,
data: data
}
}
}
}
}
};

describe('postgresqlLogCheckpoints', function () {
describe('run', function () {
it('should give unknown result if a sql instance error is passed or no data is present', function (done) {
const callback = (err, results) => {
expect(results.length).to.be.above(0);
expect(results[0].status).to.equal(3);
expect(results[0].message).to.include('Unable to query SQL instances');
expect(results[0].region).to.equal('global');
done()
};

const cache = createCache(
['error'],
null,
);

plugin.run(cache, {}, callback);
});

it('should give passing result if no sql instances are found', function (done) {
const callback = (err, results) => {
expect(results.length).to.be.above(0);
expect(results[0].status).to.equal(0);
expect(results[0].message).to.include('No SQL instances found');
expect(results[0].region).to.equal('global');
done()
};

const cache = createCache(
null,
[],
);

plugin.run(cache, {}, callback);
});

it('should give passing result if instance has log_checkpoints flag enabled', function (done) {
const callback = (err, results) => {
expect(results.length).to.be.above(0);
expect(results[0].status).to.equal(0);
expect(results[0].message).to.include('PostgreSQL instance has log_checkpoints flag enabled');
expect(results[0].region).to.equal('global');
done()
};

const cache = createCache(
null,
[
{
"kind": "sql#instance",
"state": "RUNNABLE",
"databaseVersion": "POSTGRES_13",
"settings": {
"authorizedGaeApplications": [],
"tier": "db-custom-1-3840",
"kind": "sql#settings",
"availabilityType": "ZONAL",
"pricingPlan": "PER_USE",
"replicationType": "SYNCHRONOUS",
"activationPolicy": "ALWAYS",
"locationPreference": {
"zone": "us-central1-f",
"kind": "sql#locationPreference"
},
"databaseFlags": [
{
"name": "log_checkpoints",
"value": "on"
}
],
"dataDiskType": "PD_HDD",
"maintenanceWindow": {
"kind": "sql#maintenanceWindow",
"hour": 0,
"day": 0
},
"settingsVersion": "3",
"storageAutoResizeLimit": "0",
"storageAutoResize": false,
"dataDiskSizeGb": "10"
},
"instanceType": "CLOUD_SQL_INSTANCE",
"name": "aqua-instance",
"region": "us-central1",
"gceZone": "us-central1-f"
}
]
);

plugin.run(cache, {}, callback);
});

it('should give failing result if instance does not have log_checkpoints flag enabled', function (done) {
const callback = (err, results) => {
expect(results.length).to.be.above(0);
expect(results[0].status).to.equal(2);
expect(results[0].message).to.include('PostgreSQL instance does not have log_checkpoints flag enabled');
expect(results[0].region).to.equal('global');
done()
};

const cache = createCache(
null,
[
{
"kind": "sql#instance",
"state": "RUNNABLE",
"databaseVersion": "POSTGRES_13",
"settings": {
"authorizedGaeApplications": [],
"tier": "db-custom-1-3840",
"kind": "sql#settings",
"availabilityType": "ZONAL",
"pricingPlan": "PER_USE",
"replicationType": "SYNCHRONOUS",
"activationPolicy": "ALWAYS",
"locationPreference": {
"zone": "us-central1-f",
"kind": "sql#locationPreference"
},
"databaseFlags": [
{
"name": "log_checkpoints",
"value": "off"
}
],
"dataDiskType": "PD_HDD",
"maintenanceWindow": {
"kind": "sql#maintenanceWindow",
"hour": 0,
"day": 0
},
"settingsVersion": "3",
"storageAutoResizeLimit": "0",
"storageAutoResize": false,
"dataDiskSizeGb": "10"
},
"instanceType": "CLOUD_SQL_INSTANCE",
"name": "aqua-instance",
"region": "us-central1",
"gceZone": "us-central1-f"
}
]
);

plugin.run(cache, {}, callback);
})

it('should give failing result if instance does not support log_checkpoints flag', function (done) {
const callback = (err, results) => {
expect(results.length).to.be.above(0);
expect(results[0].status).to.equal(0);
expect(results[0].message).to.include('SQL instance database version is not of PosgreSQL type');
expect(results[0].region).to.equal('global');
done()
};

const cache = createCache(
null,
[
{
"kind": "sql#instance",
"state": "RUNNABLE",
"databaseVersion": "MYSQL_5_7",
"settings": {
"authorizedGaeApplications": [],
"tier": "db-custom-1-3840",
"kind": "sql#settings",
"availabilityType": "ZONAL",
"pricingPlan": "PER_USE",
"replicationType": "SYNCHRONOUS",
"activationPolicy": "ALWAYS",
"locationPreference": {
"zone": "us-central1-f",
"kind": "sql#locationPreference"
},
"databaseFlags": [],
"dataDiskType": "PD_HDD",
"maintenanceWindow": {
"kind": "sql#maintenanceWindow",
"hour": 0,
"day": 0
},
"settingsVersion": "3",
"storageAutoResizeLimit": "0",
"storageAutoResize": false,
"dataDiskSizeGb": "10"
},
"instanceType": "CLOUD_SQL_INSTANCE",
"name": "aqua-instance",
"region": "us-central1",
"gceZone": "us-central1-f"
}
]
);

plugin.run(cache, {}, callback);
})
})
})