Skip to content

Commit a7dcdc9

Browse files
RamSubbaraoGitHub Enterprise
authored andcommitted
APAR IT46430 (#673)
* APAR IT4643 * Address review comments
1 parent 04201bb commit a7dcdc9

File tree

4 files changed

+86
-17
lines changed

4 files changed

+86
-17
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fred:$2y$05$3Fp9
1+
passw0rd

authservice/mqsimpleauth/src/simpleauth.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ int simpleauth_authenticate_user(char *user, char *password)
3131
if (simpleauth_valid_user(user))
3232
{
3333
char *pwd = getSecretForUser(user);
34-
if(pwd != NULL)
35-
{
36-
int pwdCheck = strncmp(pwd, password, strlen(password));
34+
if (pwd != NULL)
35+
{
36+
int pwdCheck = strcmp(pwd, password);
3737
if (pwdCheck == 0)
3838
{
3939
log_debugf("Correct password supplied. user=%s", user);
@@ -44,7 +44,7 @@ int simpleauth_authenticate_user(char *user, char *password)
4444
log_debugf("Incorrect password supplied. user=%s", user);
4545
result = SIMPLEAUTH_INVALID_PASSWORD;
4646
}
47-
pwd = NULL;
47+
free(pwd);
4848
}
4949
else
5050
{
@@ -80,12 +80,17 @@ char *getSecretForUser(char *user)
8080
}
8181
else
8282
{
83-
char* pwdFromEnv = getenv("MQ_APP_PASSWORD");
84-
if (pwdFromEnv != NULL)
83+
char* envValue = getenv("MQ_APP_PASSWORD");
84+
if (envValue != NULL)
8585
{
8686
log_infof("Environment variable MQ_APP_PASSWORD is deprecated, use secrets to set the passwords");
87+
char* pwdFromEnv = strdup(envValue);
88+
return pwdFromEnv;
89+
}
90+
else
91+
{
92+
return NULL;
8793
}
88-
return pwdFromEnv;
8994
}
9095
} else if (0 == strcmp(user, ADMIN_USER_NAME))
9196
{
@@ -96,12 +101,18 @@ char *getSecretForUser(char *user)
96101
}
97102
else
98103
{
99-
char* pwdFromEnv = getenv("MQ_ADMIN_PASSWORD");
100-
if (pwdFromEnv != NULL)
104+
char* envValue = getenv("MQ_ADMIN_PASSWORD");
105+
if (envValue != NULL)
101106
{
102107
log_infof("Environment variable MQ_ADMIN_PASSWORD is deprecated, use secrets to set the passwords");
108+
// Get the value of environment variable and store it as a copy to free up the memory
109+
char* pwdFromEnv = strdup(envValue);
110+
return pwdFromEnv;
111+
}
112+
else
113+
{
114+
return NULL;
103115
}
104-
return pwdFromEnv;
105116
}
106117
}
107118
else
@@ -117,7 +128,10 @@ char *readSecret(char* secret)
117128
if (fp)
118129
{
119130
char *pwd = malloc(line_size);
120-
fgets(pwd, line_size, fp);
131+
char *result = fgets(pwd, line_size, fp);
132+
if (result == NULL)
133+
return NULL;
134+
121135
fclose(fp);
122136
return pwd;
123137
}

authservice/mqsimpleauth/src/simpleauth_test.c

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ void test_read_secret_ok()
4545
{
4646
test_start();
4747
char *pwd = readSecret("./src/mqAdminPassword");
48-
char *password = "fred:$2y$05$3Fp9";
49-
if (0 == strncmp(pwd, password, strlen(password)))
48+
char *password = "passw0rd";
49+
if (0 == strcmp(pwd, password))
5050
test_pass();
5151
else
5252
test_fail(__func__);
@@ -120,6 +120,49 @@ void test_simpleauth_authenticate_user_admin_ok()
120120
test_pass();
121121
}
122122

123+
void test_simpleauth_authenticate_user_app_invalidpwd()
124+
{
125+
test_start();
126+
char *password[] = {"passw0r", "pass", "passw0rd1", "NULL", "","password123"};
127+
setenv("MQ_APP_PASSWORD", "passw0rd", 1);
128+
129+
for(int i=0; i< (sizeof(password)/sizeof(password[0])); ++i)
130+
{
131+
int rc = simpleauth_authenticate_user("app", password[i]);
132+
printf("%s: Validating app user with password set to %s and rc is %d\n", __func__,password[i], rc);
133+
if (rc != SIMPLEAUTH_INVALID_PASSWORD)
134+
test_fail(__func__);
135+
}
136+
test_pass();
137+
}
138+
139+
void test_simpleauth_authenticate_user_admin_invalidpwd()
140+
{
141+
test_start();
142+
char *password[] = {"passw0r", "pass", "passw0rd1", "NULL", "","password123"};
143+
setenv("MQ_ADMIN_PASSWORD", "passw0rd", 1);
144+
145+
for(int i=0; i< (sizeof(password)/sizeof(password[0])); ++i)
146+
{
147+
int rc = simpleauth_authenticate_user("admin", password[i]);
148+
printf("%s: validating admin user with password set to %s and rc is %d\n", __func__,password[i], rc);
149+
if (rc != SIMPLEAUTH_INVALID_PASSWORD)
150+
test_fail(__func__);
151+
}
152+
test_pass();
153+
}
154+
155+
void test_simpleauth_authenticate_user_admin_with_null_pwd()
156+
{
157+
test_start();
158+
setenv("MQ_ADMIN_PASSWORD", "", 1);
159+
int rc = simpleauth_authenticate_user("admin", "passw0rd");
160+
printf("%s: admin - %d\n", __func__, rc);
161+
if (rc == SIMPLEAUTH_VALID)
162+
test_fail(__func__);
163+
test_pass();
164+
}
165+
123166
void test_simpleauth_authenticate_user_admin_invalidpassword()
124167
{
125168
test_start();
@@ -131,6 +174,17 @@ void test_simpleauth_authenticate_user_admin_invalidpassword()
131174
test_pass();
132175
}
133176

177+
void test_simpleauth_authenticate_user_admin_invalishortdpassword()
178+
{
179+
test_start();
180+
setenv("MQ_ADMIN_PASSWORD", "password", 1);
181+
int rc = simpleauth_authenticate_user("admin", "pass");
182+
printf("%s: admin - %d\n", __func__, rc);
183+
if (rc != SIMPLEAUTH_INVALID_PASSWORD)
184+
test_fail(__func__);
185+
test_pass();
186+
}
187+
134188

135189
// ----------------------------------------------------------------------------
136190
// Multi-threaded test
@@ -220,15 +274,18 @@ int main()
220274
// Turn on debugging for the tests
221275
setenv("DEBUG", "true", true);
222276
log_init("simpleauth_test.log");
223-
224277
test_read_secret_ok();
278+
test_simpleauth_authenticate_user_admin_invalidpwd();
279+
test_simpleauth_authenticate_user_app_invalidpwd();
225280
test_simpleauth_valid_user_app_valid();
226281
test_simpleauth_valid_user_admin_valid();
227282
test_simpleauth_valid_user_george_invalid();
228283
test_simpleauth_authenticate_user_fred_unknown();
229284
test_simpleauth_authenticate_user_app_ok();
285+
test_simpleauth_authenticate_user_admin_with_null_pwd();
230286
test_simpleauth_authenticate_user_admin_ok();
231287
test_simpleauth_authenticate_user_admin_invalidpassword();
288+
test_simpleauth_authenticate_user_admin_invalishortdpassword();
232289

233290
log_close();
234291

authservice/mqsimpleauth/src/simpleauth_test_invalid.passwd

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)