Creating a user and login or connect new user to existing sql server login with azure cli command? - authentication

I was just wondering if there is a way to create a user and either creating or connect that user to an existing azure sql server login using azure cli?
EDIT:
I would do it with something like this in sql, for example:
-- For login login_name, create a user in the database
CREATE USER <username> FOR LOGIN <login> WITH DEFAULT_SCHEMA = [dbo]
GO
-- Add user to the database owner role
EXEC sp_addrolemember N'db_owner', N'<username>'
GO

You can use below command to run sql query in Azure PowerShell and I followed SO-Thread:
$servername = "sqlservername.database.windows.net"
$databasename = "databasename"
$username = "rithwik"
$pass = "R!th"
$query = "CREATE USER User2 For login xxx WITH DEFAULT_SCHEMA = [dbo]"
$sqlCon = New-Object System.Data.SqlClient.SqlConnection
$sqlCon.ConnectionString = "Server=$servername;Database=$databasename;User ID=$username;Password=$pass;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
$sqlCon.Open()
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand($query, $sqlCon)
$sqlCmd.ExecuteNonQuery()
$sqlCon.Close()
Here xxx is the login name.
Then the user is created:
Then you use
$query = "EXEC sp_addrolemember N'db_or', N'<username>'" in place of above $query to add role.

Related

Databricks online store - Login to Azure SQL Database with Service Principal

I want to use Databricks Online Store with Azure SQL Database, however I am unable to autenthicate through Databricks Feature Store API. I need to use Service Principal credentials.
I tried using Application ID as username and Secret as password ( com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user '[REDACTED]'. ClientConnectionId:some-id-x-x-), but no luck.
I also tried to generate AAD access Token and use it as a password, however I am getting password exceeds maximum length of 128characters...
When I use the same credentials to test it via JayDeBeApi everything works...
Code I am using:
from databricks.feature_store.online_store_spec import AzureSqlServerSpec
from databricks.feature_store import FeatureStoreClient
username = "application-id"
password = "application-secret"
tenantId = "tenant-id"
server_name = "server-name.database.windows.net"
port = "1433"
db_name = "database-name"
fs = FeatureStoreClient()
online_store = AzureSqlServerSpec(
hostname=server_name,
port=1433,
database_name=db_name,
user=username,
password=password,
table_name="TableName",
)
fs.publish_table(
name='feature_store.TableName',
online_store=online_store,
mode='merge'
)

SQL statement to Create Role fails on Postgres 12 using Dapper

I am running Postgres 12 on Windows and have a .Net Core app which uses Dapper as an ORM:
The following query works fine:
var sql = "SELECT 1 FROM pg_roles WHERE rolname=#un"
var result = con.ExecuteScalar<int>(sql, new {un = "someuser"});
Now I'm trying to execute an sql statement that would create a role:
var sql = #"CREATE ROLE #un WITH LOGIN NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION PASSWORD #pw";
con.Execute(sql, new {un = "someuser", pw = "somepass");
This query fails with the following exception: Npgsql.PostgresException: '42601: syntax error at or near "$1"'.
What am I missing here?
The name of a role is an identifier. You can not pass identifiers as parameters, you will need to concatenate the name as a constant value into the SQL string, so that you execute this:
var sql = #"CREATE ROLE someuser WITH LOGIN NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION PASSWORD #pw";
con.Execute(sql, new {pw = "somepass");
I am not entirely sure sending the password as a parameter works either. You will need to test that.

Confused on how to use grant statement to make another login with password

create login sea with password = 'Atlantic-ocean';
A GRANT statement to execute if we allow the above 'sea'
login to create another login called 'jellyfish' with a password 'Jelly-fish'.
create user jellyfish for login sea;
grant alter user jellyfish
with password = 'Jelly-fish';
msg 33234 parameter password cannot be provided for users that cannot
authenticate in a database.

How to query a Google Cloud SQL Database and display results as html?

I have a Google Cloud SQL Database. I want to run a static SQL query on the database and display results as html page without using a third party web server.
Any suggestions?
You can query your Cloud SQL database just like you would any MySQL database. you just need to make sure you tell Cloud SQL the IP from which you're going to connect:
or configure your Cloud SQL instance to use a static IP, more about that here.
Then you connect as per normal (this is some PHP taken from here):
$servername = "<The IP address from the overview page of Cloud SQL>";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

Any hidden problems exists if i set CHECK_POLICY = OFF

I have a Store procedure to create auto generate SQl users.
code to create User
CREATE LOGIN userName with PASSWORD= 'autoGeneratedPass'
Problem: When the auto generated Password is weak then i got the below error
Password validation failed. The password does not meet Windows policy requirements because it is too short.
so i turned off Windows policy checking
Code
CREATE LOGIN userName with PASSWORD= 'autoGeneratedPass' CHECK_POLICY = OFF
If i did that any hidden problem arise?