use of org.apache.zeppelin.user.UsernamePassword in project zeppelin by apache.
the class JDBCInterpreterTest method getUserAuth.
private AuthenticationInfo getUserAuth(String user, String entityName, String dbUser, String dbPassword) {
UserCredentials userCredentials = new UserCredentials();
if (entityName != null && dbUser != null && dbPassword != null) {
UsernamePassword up = new UsernamePassword(dbUser, dbPassword);
userCredentials.putUsernamePassword(entityName, up);
}
AuthenticationInfo authInfo = new AuthenticationInfo();
authInfo.setUserCredentials(userCredentials);
authInfo.setUser(user);
return authInfo;
}
use of org.apache.zeppelin.user.UsernamePassword in project zeppelin by apache.
the class CredentialInjectorTest method replaceCredentials.
@Test
public void replaceCredentials() {
UserCredentials userCredentials = mock(UserCredentials.class);
UsernamePassword usernamePassword = new UsernamePassword("username", "pwd");
when(userCredentials.getUsernamePassword("mysql")).thenReturn(usernamePassword);
CredentialInjector testee = new CredentialInjector(userCredentials);
String actual = testee.replaceCredentials(TEMPLATE);
assertEquals(CORRECT_REPLACED, actual);
InterpreterResult ret = new InterpreterResult(Code.SUCCESS, ANSWER);
InterpreterResult hiddenResult = testee.hidePasswords(ret);
assertEquals(1, hiddenResult.message().size());
assertEquals(HIDDEN, hiddenResult.message().get(0).getData());
}
use of org.apache.zeppelin.user.UsernamePassword in project zeppelin by apache.
the class ParagraphTest method credentialReplacement.
// (TODO zjffdu) temporary disable it.
// https://github.com/apache/zeppelin/pull/3416
@Ignore
@Test
public void credentialReplacement() throws Throwable {
Note mockNote = mock(Note.class);
Credentials creds = mock(Credentials.class);
when(mockNote.getCredentials()).thenReturn(creds);
Paragraph spyParagraph = spy(new Paragraph("para_1", mockNote, null));
UserCredentials uc = mock(UserCredentials.class);
when(creds.getUserCredentials(anyString())).thenReturn(uc);
UsernamePassword up = new UsernamePassword("user", "pwd");
when(uc.getUsernamePassword("ent")).thenReturn(up);
Interpreter mockInterpreter = mock(Interpreter.class);
spyParagraph.setInterpreter(mockInterpreter);
doReturn(mockInterpreter).when(spyParagraph).getBindedInterpreter();
ManagedInterpreterGroup mockInterpreterGroup = mock(ManagedInterpreterGroup.class);
when(mockInterpreter.getInterpreterGroup()).thenReturn(mockInterpreterGroup);
when(mockInterpreterGroup.getId()).thenReturn("mock_id_1");
when(mockInterpreterGroup.getAngularObjectRegistry()).thenReturn(mock(AngularObjectRegistry.class));
when(mockInterpreterGroup.getResourcePool()).thenReturn(mock(ResourcePool.class));
when(mockInterpreter.getFormType()).thenReturn(FormType.NONE);
ParagraphJobListener mockJobListener = mock(ParagraphJobListener.class);
doReturn(mockJobListener).when(spyParagraph).getListener();
InterpreterResult mockInterpreterResult = mock(InterpreterResult.class);
when(mockInterpreter.interpret(anyString(), Mockito.<InterpreterContext>any())).thenReturn(mockInterpreterResult);
when(mockInterpreterResult.code()).thenReturn(Code.SUCCESS);
AuthenticationInfo user1 = new AuthenticationInfo("user1");
spyParagraph.setAuthenticationInfo(user1);
spyParagraph.setText("val x = \"usr={user.ent}&pass={password.ent}\"");
// Credentials should only be injected when it is enabled for an interpreter or when specified in a local property
when(mockInterpreter.getProperty(Constants.INJECT_CREDENTIALS, "false")).thenReturn("false");
spyParagraph.jobRun();
verify(mockInterpreter).interpret(eq("val x = \"usr={user.ent}&pass={password.ent}\""), any(InterpreterContext.class));
when(mockInterpreter.getProperty(Constants.INJECT_CREDENTIALS, "false")).thenReturn("true");
mockInterpreter.setProperty(Constants.INJECT_CREDENTIALS, "true");
spyParagraph.jobRun();
verify(mockInterpreter).interpret(eq("val x = \"usr=user&pass=pwd\""), any(InterpreterContext.class));
// Check if local property override works
when(mockInterpreter.getProperty(Constants.INJECT_CREDENTIALS, "false")).thenReturn("true");
spyParagraph.getLocalProperties().put(Constants.INJECT_CREDENTIALS, "true");
spyParagraph.jobRun();
verify(mockInterpreter).interpret(eq("val x = \"usr=user&pass=pwd\""), any(InterpreterContext.class));
}
use of org.apache.zeppelin.user.UsernamePassword in project SSM by Intel-bigdata.
the class CredentialRestApi method putCredentials.
/**
* Put User Credentials REST API
* @param message - JSON with entity, username, password.
* @return JSON with status.OK
* @throws IOException, IllegalArgumentException
*/
@PUT
public Response putCredentials(String message) throws IOException, IllegalArgumentException {
Map<String, String> messageMap = gson.fromJson(message, new TypeToken<Map<String, String>>() {
}.getType());
String entity = messageMap.get("entity");
String username = messageMap.get("username");
String password = messageMap.get("password");
if (Strings.isNullOrEmpty(entity) || Strings.isNullOrEmpty(username) || Strings.isNullOrEmpty(password)) {
return new JsonResponse(Status.BAD_REQUEST).build();
}
String user = SecurityUtils.getPrincipal();
logger.info("Update credentials for user {} entity {}", user, entity);
UserCredentials uc = credentials.getUserCredentials(user);
uc.putUsernamePassword(entity, new UsernamePassword(username, password));
credentials.putUserCredentials(user, uc);
return new JsonResponse(Status.OK).build();
}
use of org.apache.zeppelin.user.UsernamePassword in project zeppelin by apache.
the class CredentialInjector method replaceCredentials.
public String replaceCredentials(String code) {
if (code == null) {
return null;
}
String replaced = code;
Matcher matcher = userpattern.matcher(replaced);
while (matcher.find()) {
String key = matcher.group(1);
UsernamePassword usernamePassword = creds.getUsernamePassword(key);
if (usernamePassword != null) {
String value = usernamePassword.getUsername();
String quotedValue = Matcher.quoteReplacement(value);
replaced = matcher.replaceFirst(quotedValue);
matcher = userpattern.matcher(replaced);
}
}
matcher = passwordpattern.matcher(replaced);
while (matcher.find()) {
String key = matcher.group(1);
UsernamePassword usernamePassword = creds.getUsernamePassword(key);
if (usernamePassword != null) {
passwords.add(usernamePassword.getPassword());
String value = usernamePassword.getPassword();
String quotedValue = Matcher.quoteReplacement(value);
replaced = matcher.replaceFirst(quotedValue);
matcher = passwordpattern.matcher(replaced);
}
}
return replaced;
}
Aggregations