Search in sources :

Example 1 with UsernamePassword

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;
}
Also used : UserCredentials(org.apache.zeppelin.user.UserCredentials) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) UsernamePassword(org.apache.zeppelin.user.UsernamePassword)

Example 2 with UsernamePassword

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());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) UserCredentials(org.apache.zeppelin.user.UserCredentials) UsernamePassword(org.apache.zeppelin.user.UsernamePassword) Test(org.junit.Test)

Example 3 with UsernamePassword

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));
}
Also used : Interpreter(org.apache.zeppelin.interpreter.Interpreter) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ResourcePool(org.apache.zeppelin.resource.ResourcePool) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) UsernamePassword(org.apache.zeppelin.user.UsernamePassword) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup) UserCredentials(org.apache.zeppelin.user.UserCredentials) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) UserCredentials(org.apache.zeppelin.user.UserCredentials) Credentials(org.apache.zeppelin.user.Credentials) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) Ignore(org.junit.Ignore) AbstractInterpreterTest(org.apache.zeppelin.interpreter.AbstractInterpreterTest) Test(org.junit.Test)

Example 4 with UsernamePassword

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();
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) UserCredentials(org.apache.zeppelin.user.UserCredentials) JsonResponse(org.apache.zeppelin.server.JsonResponse) UsernamePassword(org.apache.zeppelin.user.UsernamePassword)

Example 5 with UsernamePassword

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;
}
Also used : Matcher(java.util.regex.Matcher) UsernamePassword(org.apache.zeppelin.user.UsernamePassword)

Aggregations

UsernamePassword (org.apache.zeppelin.user.UsernamePassword)7 UserCredentials (org.apache.zeppelin.user.UserCredentials)5 TypeToken (com.google.gson.reflect.TypeToken)2 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)2 JsonResponse (org.apache.zeppelin.server.JsonResponse)2 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 Matcher (java.util.regex.Matcher)1 PUT (javax.ws.rs.PUT)1 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)1 AbstractInterpreterTest (org.apache.zeppelin.interpreter.AbstractInterpreterTest)1 Interpreter (org.apache.zeppelin.interpreter.Interpreter)1 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)1 ManagedInterpreterGroup (org.apache.zeppelin.interpreter.ManagedInterpreterGroup)1 ResourcePool (org.apache.zeppelin.resource.ResourcePool)1 Credentials (org.apache.zeppelin.user.Credentials)1 Ignore (org.junit.Ignore)1