Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
SQL Failover Group - Add a provisioning parameter to specify user rol…
Browse files Browse the repository at this point in the history
…es (#175)

* revert to use _.each in lifecycle

* add a provisioining parameter to specify the roles of user

* fix async.whilst in binding

* update the doc

* fix tile yml file

* set the grace period to 1 minute

* roll grace period back to 60 minutes

* fix typo
  • Loading branch information
zhongyi-zhang authored May 16, 2018
1 parent efddd50 commit 58c1455
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 26 deletions.
18 changes: 16 additions & 2 deletions docs/azure-sql-db-failover-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@

1. Login to the primary database, create a new user with password.

2. Grant permission "CONTROL" to the user.
2. Alter roles (default to db_owner) to the user.

 3. Collect [credentials](./azure-sql-db-failover-group.md#format-of-credentials).

**NOTE:**

* Binding would fail after failover because of the change of the primary role. Please bind before failover or fail back to bind. If you do have a case which has to bind after failover, please open a Github issue to request our improvement.

* Permission "CONTROL" has full permissions in a database: https://msdn.microsoft.com/en-us/library/ms178569.aspx
* See details about fixed roles: https://docs.microsoft.com/en-us/sql/relational-databases/security/authentication-access/database-level-roles?view=sql-server-2017#fixed-database-roles.

### Unbind

Expand Down Expand Up @@ -95,6 +95,20 @@
}
```

And here is an optional provisioning parameter `userRoles` you can add to the json file, to specify the roles of the new users created in binding. If not present, the default role is db_owner. More details about roles: https://docs.microsoft.com/en-us/sql/relational-databases/security/authentication-access/database-level-roles?view=sql-server-2017#fixed-database-roles.

For example,

```
{
"primaryServerName": "sqlservera",
"primaryDbName": "sqldba",
"secondaryServerName": "sqlserverb",
"failoverGroupName": "failovergroupa",
"userRoles": ["db_datareader", "db_datawriter"]
}
```

**NOTE:**

* Again, this module assumes you already have two existing servers and the target database on the primary server to get geo-replication, and these servers are provided in the meta service broker manifest file. See the "Modules related configurations" section [here](https://github.com/Azure/meta-azure-service-broker/blob/master/docs/how-admin-deploy-the-broker.md#deploy-the-meta-azure-service-broker-as-an-application-in-cloud-foundry) for details. For example, the above provisioning parameters should have the following servers provided in the broker manifest:
Expand Down
12 changes: 7 additions & 5 deletions examples/sqldb-failover-group-example-config.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/*
{
"primaryServerName": "<sql-server-name>", // [Required] The resource group name, admin login, and admin login password of this server should be provided in broker manifest.
"primaryDbName": "<sql-database-name>", // [Required] The existing database on the primary server.
"secondaryServerName": "<sql-server-name>", // [Required] The resource group name, admin login, and admin login password of this server should be provided in broker manifest.
"failoverGroupName": "<failover-group-name>" // [Required] The name of the new failover group.
"primaryServerName": "<sql-server-name>", // [Required] The resource group name, admin login, and admin login password of this server should be provided in broker manifest.
"primaryDbName": "<sql-database-name>", // [Required] The existing database on the primary server.
"secondaryServerName": "<sql-server-name>", // [Required] The resource group name, admin login, and admin login password of this server should be provided in broker manifest.
"failoverGroupName": "<failover-group-name>", // [Required] The name of the new failover group.
"userRoles": <an-array-of-fixed-roles> // [Optional] If not present, default to db_owner in the broker manifest. See about fixed roles: https://docs.microsoft.com/en-us/sql/relational-databases/security/authentication-access/database-level-roles?view=sql-server-2017#fixed-database-roles.
}
*/

Expand All @@ -12,5 +13,6 @@
"primaryServerName": "sqlservera",
"primaryDbName": "sqldba",
"secondaryServerName": "sqlserverb",
"failoverGroupName": "failovergroupa"
"failoverGroupName": "failovergroupa",
"userRoles": ["db_datareader", "db_datawriter"]
}
3 changes: 2 additions & 1 deletion lib/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ var validateConfigurations = function() {
'DEFAULT_PARAMETERS_AZURE_COSMOSDB',
'DEFAULT_PARAMETERS_AZURE_MYSQLDB',
'DEFAULT_PARAMETERS_AZURE_POSTGRESQLDB',
'DEFAULT_PARAMETERS_AZURE_SQLDB'
'DEFAULT_PARAMETERS_AZURE_SQLDB',
'DEFAULT_PARAMETERS_AZURE_SQLDB_FAILOVER_GROUP'
];
envVarShouldBeJSON.forEach(function(envVarName) {
if (process.env[envVarName]) {
Expand Down
4 changes: 2 additions & 2 deletions lib/services/azuresqldbfailovergroup/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ sqldbfgOperations.prototype.dropDatabaseUser = function (serverDomainName, admin
that.executeSql(config, sql, callback);
};

sqldbfgOperations.prototype.grantControlToUser = function (serverDomainName, adminLogin, adminLoginPassword, databaseUser, callback) {
sqldbfgOperations.prototype.alterRoleToUser = function (serverDomainName, adminLogin, adminLoginPassword, databaseUser, roleName, callback) {
var that = this;
var config = getDbConnectionConfig(serverDomainName, adminLogin, adminLoginPassword, that.dbName);
var sql = util.format('GRANT CONTROL to "%s"', databaseUser);
var sql = util.format('ALTER ROLE %s ADD MEMBER "%s"', roleName, databaseUser);
that.executeSql(config, sql, callback);
};

Expand Down
31 changes: 23 additions & 8 deletions lib/services/azuresqldbfailovergroup/cmd-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var sqldbfgBind = function (params) {
var secondaryResourceGroupName = provisioningResult.secondaryResourceGroupName || '';
var secondaryServerName = reqParams.secondaryServerName || '';
var failoverGroupName = reqParams.failoverGroupName || '';
var userRoles = reqParams.userRoles || '';

log.info(util.format('cmd-bind: primaryResourceGroupName: %s, primaryServerName: %s, primaryDbName: %s', primaryResourceGroupName, primaryServerName, primaryDbName));

Expand Down Expand Up @@ -61,15 +62,29 @@ var sqldbfgBind = function (params) {
);
},
function (callback) {
sqldbfgOperations.grantControlToUser(
provisioningResult.primaryFQDN,
primaryAdministratorLogin,
primaryAdministratorLoginPassword,
databaseUser,
var n = userRoles.length;
var i = 0;
async.whilst(
function() {
return i < n;
},
function(cb) {
sqldbfgOperations.alterRoleToUser(
provisioningResult.primaryFQDN,
primaryAdministratorLogin,
primaryAdministratorLoginPassword,
databaseUser,
userRoles[i],
function(err) {
if (err) {
log.error('cmd-bind: async.waterfall/alterRoleToUser: err: %j', err);
}
i++;
cb(err);
}
);
},
function(err) {
if (err) {
log.error('cmd-bind: async.waterfall/grantControlToUser: err: %j', err);
}
callback(err);
}
);
Expand Down
4 changes: 4 additions & 0 deletions lib/services/azuresqldbfailovergroup/cmd-provision.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var sqldbfgProvision = function (params) {
var primaryDbName = reqParams.primaryDbName || '';
var secondaryServerName = reqParams.secondaryServerName || '';
var failoverGroupName = reqParams.failoverGroupName || '';
var userRoles = reqParams.userRoles || '';

this.provision = function (sqldbfgOperations, next) {
var primaryFQDN, secondaryFQDN;
Expand Down Expand Up @@ -172,6 +173,9 @@ var sqldbfgProvision = function (params) {
}
}

if (!_.isArray(userRoles)) {
ret.push('userRoles');
}
return ret;
};

Expand Down
3 changes: 2 additions & 1 deletion lib/services/azuresqldbfailovergroup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var uuid = require('uuid');

var Handlers = {};

Handlers.fixParameters = function(parameters, accountPool) {
Handlers.fixParameters = function(parameters) {
parameters = common.fixParametersWithDefaults('DEFAULT_PARAMETERS_AZURE_SQLDB_FAILOVER_GROUP', parameters);
return parameters;
};

Expand Down
3 changes: 3 additions & 0 deletions manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,6 @@ applications:
}
}
}'
DEFAULT_PARAMETERS_AZURE_SQLDB_FAILOVER_GROUP: '{
"userRoles": ["db_owner"]
}'
4 changes: 2 additions & 2 deletions pcf-tile/tile-history.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ history:
- 1.2.1
- 1.3.0
- 1.4.0
- 1.5.0
version: 1.5.1
- 1.5.1
version: 1.5.2
8 changes: 8 additions & 0 deletions pcf-tile/tile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ forms:
}
}
}
label: Default Parameters of Azure SQL Database Failover Group service
- name: default_parameters_azure_sqldb_failover_group
type: text
configurable: true
default: |
{
"userRoles": ["db_owner"]
}
label: Default Parameters of Azure SQL Database service

# Add any dependencies your tile has on other installed products.
Expand Down
3 changes: 2 additions & 1 deletion test/integration/submatrix/sqldbfg.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ azuresqldbfg = {
'primaryServerName': primaryServerName,
'primaryDbName': primaryDbName,
'secondaryServerName': secondaryServerName,
'failoverGroupName': failoverGroupName
'failoverGroupName': failoverGroupName,
'userRoles': ['db_owner']
},
bindingParameters: {},
credentials: {
Expand Down
11 changes: 7 additions & 4 deletions test/unit/services/azuresqldbfailovergroup/provision-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ describe('SqlDbFailoverGroup - Provision - PreConditions', function () {
'primaryServerName': 'fakeservera',
'primaryDbName': 'sqldba',
'secondaryServerName': 'fakeserverb',
'failoverGroupName': 'failovergroupa'
'failoverGroupName': 'failovergroupa',
'userRoles': ['db_owner']
},
azure: azure,
accountPool: {
Expand Down Expand Up @@ -71,7 +72,8 @@ describe('SqlDbFailoverGroup - Provision - PreConditions', function () {
'primaryServerName': 'fakeservera',
'primaryDbName': 'sqldba',
'secondaryServerName': 'fakeserverb',
'failoverGroupName': 'failovergroupa'
'failoverGroupName': 'failovergroupa',
'userRoles': ['db_owner']
},
azure: azure,
accountPool: {}
Expand Down Expand Up @@ -101,12 +103,13 @@ describe('SqlDbFailoverGroup - Provision - PreConditions', function () {
});

it('should fail to validate the parameters', function () {
(cp.getInvalidParams().length).should.equal(4);
(cp.getInvalidParams().length).should.equal(5);
cp.getInvalidParams().should.deepEqual([
'primaryServerName',
'primaryDbName',
'secondaryServerName',
'failoverGroupName'
'failoverGroupName',
'userRoles'
]);
});
});
Expand Down

0 comments on commit 58c1455

Please sign in to comment.