Skip to content

Commit

Permalink
More specific error message on creation of windows login for NT Servi…
Browse files Browse the repository at this point in the history
…ce domain (babelfish-for-postgresql#2828) (babelfish-for-postgresql#2921)

Currently in babelfish, when a new windows login is created for NT Service domain - it raises an error on space character. With this change babelfish will check for the NT Service domain and print an error specifically indicating this is not supported in Babelfish and added relevant test cases. NT Service may be spelled in mixed case.

Issues Resolved: BABEL-4212

Signed-off-by: P Aswini Kumar <[email protected]>
  • Loading branch information
aswiniip authored Sep 11, 2024
1 parent ba1055d commit 94833f2
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 0 deletions.
12 changes: 12 additions & 0 deletions contrib/babelfishpg_tsql/src/pl_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -2615,6 +2615,8 @@ bbf_ProcessUtility(PlannedStmt *pstmt,

if (from_windows && orig_loginname)
{
char* domain_name = NULL;

/*
* The login name must contain '\' if it is
* windows login or else throw error.
Expand Down Expand Up @@ -2654,6 +2656,16 @@ bbf_ProcessUtility(PlannedStmt *pstmt,
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("'%s' is not a valid name because it contains invalid characters.", orig_loginname)));

/*
* Check whether the domain name is supported
* or not
*/
domain_name = get_windows_domain_name(orig_loginname);
if(windows_domain_is_not_supported(domain_name))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("'%s' domain is not yet supported in Babelfish.", domain_name)));

/*
* Check whether the domain name contains invalid characters or not.
*/
Expand Down
25 changes: 25 additions & 0 deletions contrib/babelfishpg_tsql/src/rolecmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -2328,6 +2328,31 @@ windows_login_contains_invalid_chars(char *input)
return false;
}

/*
* Extract the domain name from the orig_loginname
*/
char*
get_windows_domain_name(char* input){
char *pos_slash = strchr(input, '\\');
int domain_len = pos_slash - input;
char *domain_name = palloc(domain_len+1);
strncpy(domain_name, input, domain_len);
domain_name[domain_len] = '\0';
return domain_name;
}

/*
* Check whether the domain name is supported or not
*/
bool
windows_domain_is_not_supported(char* domain_name)
{
if(strcasecmp(domain_name, "nt service") == 0) {
return true;
}
return false;
}

/**
* Domain name checks, doesnot allow characters like "<>&*|quotes spaces"
* */
Expand Down
2 changes: 2 additions & 0 deletions contrib/babelfishpg_tsql/src/rolecmds.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ extern char *convertToUPN(char *input);
extern bool windows_login_contains_invalid_chars(char *input);
extern bool windows_domain_contains_invalid_chars(char *input);
extern bool check_windows_logon_length(char *input);
extern char* get_windows_domain_name(char* input);
extern bool windows_domain_is_not_supported(char* domain_name);


#endif
114 changes: 114 additions & 0 deletions test/JDBC/expected/BABEL-UNSUPPORTED.out
Original file line number Diff line number Diff line change
Expand Up @@ -2966,3 +2966,117 @@ GO
~~ERROR (Message: 'EXTERNAL NAME' is not currently supported in Babelfish)~~


-- create login from windows
-- Add 'dummydomain' domain entry
exec sys.babelfish_add_domain_mapping_entry 'dummydomain', 'dummydomain.babel';
GO

CREATE LOGIN [NT Service\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT Service' domain is not yet supported in Babelfish.)~~


CREATE LOGIN [\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: The login name '\MSSQLSERVER' is invalid. The domain can not be empty.)~~


CREATE LOGIN [NT Servicesomething\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT Servicesomething\MSSQLSERVER' is not valid because the domain name contains invalid characters.)~~


CREATE LOGIN [NT ServiceNT Service\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT ServiceNT Service\MSSQLSERVER' is not valid because the domain name contains invalid characters.)~~


CREATE LOGIN [NT ServiceNT SerViCe\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT ServiceNT SerViCe\MSSQLSERVER' is not valid because the domain name contains invalid characters.)~~


CREATE LOGIN [somethingNT Service\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'somethingNT Service\MSSQLSERVER' is not valid because the domain name contains invalid characters.)~~


CREATE LOGIN [NT Service\NT Service\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: The login name 'NT Service\NT Service\MSSQLSERVER' has invalid length. Login name length should be between 1 and 20 for windows login.)~~


CREATE LOGIN [NT S\ervice\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT S\ervice\MSSQLSERVER' is not a valid name because it contains invalid characters.)~~


CREATE LOGIN [NT Service\\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT Service\\MSSQLSERVER' is not a valid name because it contains invalid characters.)~~


CREATE LOGIN ["NT Service"\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: '"NT Service"\MSSQLSERVER' is not valid because the domain name contains invalid characters.)~~


CREATE LOGIN [[NT Service]\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: syntax error near '\' at line 1 and character position 26)~~


CREATE LOGIN [["NT Service"]\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: syntax error near '\' at line 1 and character position 28)~~


CREATE LOGIN [NT Service\MSSQLSERVER] FROM WINDOWS WITH DEFAULT_DATABASE=[test]
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT Service' domain is not yet supported in Babelfish.)~~


CREATE LOGIN [NT SerViCe\MSSQLSERVER] FROM WINDOWS WITH DEFAULT_DATABASE=[test]
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT SerViCe' domain is not yet supported in Babelfish.)~~


CREATE LOGIN [dummydomain\NT Service] FROM WINDOWS
GO

-- Dropping 'nt [email protected]'
DROP LOGIN [dummydomain\NT Service]
GO

-- Remove entry for 'dummydomain'
exec babelfish_remove_domain_mapping_entry 'dummydomain'
GO

Original file line number Diff line number Diff line change
Expand Up @@ -2974,3 +2974,117 @@ GO
~~ERROR (Message: 'EXTERNAL NAME' is not currently supported in Babelfish)~~


-- create login from windows
-- Add 'dummydomain' domain entry
exec sys.babelfish_add_domain_mapping_entry 'dummydomain', 'dummydomain.babel';
GO

CREATE LOGIN [NT Service\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT Service' domain is not yet supported in Babelfish.)~~


CREATE LOGIN [\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: The login name '\MSSQLSERVER' is invalid. The domain can not be empty.)~~


CREATE LOGIN [NT Servicesomething\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT Servicesomething\MSSQLSERVER' is not valid because the domain name contains invalid characters.)~~


CREATE LOGIN [NT ServiceNT Service\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT ServiceNT Service\MSSQLSERVER' is not valid because the domain name contains invalid characters.)~~


CREATE LOGIN [NT ServiceNT SerViCe\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT ServiceNT SerViCe\MSSQLSERVER' is not valid because the domain name contains invalid characters.)~~


CREATE LOGIN [somethingNT Service\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'somethingNT Service\MSSQLSERVER' is not valid because the domain name contains invalid characters.)~~


CREATE LOGIN [NT Service\NT Service\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: The login name 'NT Service\NT Service\MSSQLSERVER' has invalid length. Login name length should be between 1 and 20 for windows login.)~~


CREATE LOGIN [NT S\ervice\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT S\ervice\MSSQLSERVER' is not a valid name because it contains invalid characters.)~~


CREATE LOGIN [NT Service\\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT Service\\MSSQLSERVER' is not a valid name because it contains invalid characters.)~~


CREATE LOGIN ["NT Service"\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: '"NT Service"\MSSQLSERVER' is not valid because the domain name contains invalid characters.)~~


CREATE LOGIN [[NT Service]\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: syntax error near '\' at line 1 and character position 26)~~


CREATE LOGIN [["NT Service"]\MSSQLSERVER] FROM WINDOWS
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: syntax error near '\' at line 1 and character position 28)~~


CREATE LOGIN [NT Service\MSSQLSERVER] FROM WINDOWS WITH DEFAULT_DATABASE=[test]
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT Service' domain is not yet supported in Babelfish.)~~


CREATE LOGIN [NT SerViCe\MSSQLSERVER] FROM WINDOWS WITH DEFAULT_DATABASE=[test]
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: 'NT SerViCe' domain is not yet supported in Babelfish.)~~


CREATE LOGIN [dummydomain\NT Service] FROM WINDOWS
GO

-- Dropping 'nt [email protected]'
DROP LOGIN [dummydomain\NT Service]
GO

-- Remove entry for 'dummydomain'
exec babelfish_remove_domain_mapping_entry 'dummydomain'
GO

58 changes: 58 additions & 0 deletions test/JDBC/input/BABEL-UNSUPPORTED.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,64 @@ AS
EXTERNAL NAME babel_3571
GO

-- create login from windows
-- Add 'dummydomain' domain entry
exec sys.babelfish_add_domain_mapping_entry 'dummydomain', 'dummydomain.babel';
GO

CREATE LOGIN [NT Service\MSSQLSERVER] FROM WINDOWS
GO

CREATE LOGIN [\MSSQLSERVER] FROM WINDOWS
GO

CREATE LOGIN [NT Servicesomething\MSSQLSERVER] FROM WINDOWS
GO

CREATE LOGIN [NT ServiceNT Service\MSSQLSERVER] FROM WINDOWS
GO

CREATE LOGIN [NT ServiceNT SerViCe\MSSQLSERVER] FROM WINDOWS
GO

CREATE LOGIN [somethingNT Service\MSSQLSERVER] FROM WINDOWS
GO

CREATE LOGIN [NT Service\NT Service\MSSQLSERVER] FROM WINDOWS
GO

CREATE LOGIN [NT S\ervice\MSSQLSERVER] FROM WINDOWS
GO

CREATE LOGIN [NT Service\\MSSQLSERVER] FROM WINDOWS
GO

CREATE LOGIN ["NT Service"\MSSQLSERVER] FROM WINDOWS
GO

CREATE LOGIN [[NT Service]\MSSQLSERVER] FROM WINDOWS
GO

CREATE LOGIN [["NT Service"]\MSSQLSERVER] FROM WINDOWS
GO

CREATE LOGIN [NT Service\MSSQLSERVER] FROM WINDOWS WITH DEFAULT_DATABASE=[test]
GO

CREATE LOGIN [NT SerViCe\MSSQLSERVER] FROM WINDOWS WITH DEFAULT_DATABASE=[test]
GO

CREATE LOGIN [dummydomain\NT Service] FROM WINDOWS
GO

-- Dropping 'nt [email protected]'
DROP LOGIN [dummydomain\NT Service]
GO

-- Remove entry for 'dummydomain'
exec babelfish_remove_domain_mapping_entry 'dummydomain'
GO

-- INSERT BULK is No op. No point in failing this
INSERT BULK babel_3571 (
ID INT PRIMARY KEY NOT NULL IDENTITY(1,1),
Expand Down

0 comments on commit 94833f2

Please sign in to comment.