Skip to content

Commit

Permalink
support for PasswordProvider interface to enable writing druid extens…
Browse files Browse the repository at this point in the history
…ion which can get metadata store password from secured location or anywhere instead of plain text properties file
  • Loading branch information
Himanshu Gupta committed Feb 25, 2015
1 parent 7c02212 commit 126262e
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 6 deletions.
1 change: 1 addition & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Druid - a distributed column store.
Copyright 2012-2015 Metamarkets Group Inc.
Copyright 2015 Yahoo! Inc.

-------------------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.druid.metadata;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class DefaultPasswordProvider implements PasswordProvider
{
private final String password;

@JsonCreator
public static DefaultPasswordProvider fromString(String str)
{
return new DefaultPasswordProvider(str);
}

@JsonCreator
public DefaultPasswordProvider(@JsonProperty("password") String password)
{
this.password = password;
}

@Override
@JsonProperty
public String getPassword()
{
return password;
}

@Override
public String toString() {
return this.getClass().getCanonicalName();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public class MetadataStorageConnectorConfig
@JsonProperty
private String user = null;

@JsonProperty
private String password = null;
@JsonProperty("password")
private PasswordProvider passwordProvider;

public boolean isCreateTables()
{
Expand Down Expand Up @@ -71,7 +71,7 @@ public String getUser()

public String getPassword()
{
return password;
return passwordProvider == null ? null : passwordProvider.getPassword();
}

@Override
Expand All @@ -81,7 +81,7 @@ public String toString()
"createTables=" + createTables +
", connectURI='" + getConnectURI() + '\'' +
", user='" + user + '\'' +
", password=****" +
", passwordProvider=" + passwordProvider +
'}';
}
}
36 changes: 36 additions & 0 deletions common/src/main/java/io/druid/metadata/PasswordProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.druid.metadata;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;


/**
* Implement this for different ways to (optionally securely) access db passwords.
*/
@JsonTypeInfo(use= JsonTypeInfo.Id.NAME, property="type", defaultImpl = DefaultPasswordProvider.class)
@JsonSubTypes(value={
@JsonSubTypes.Type(name="default", value=DefaultPasswordProvider.class),
})
public interface PasswordProvider
{
public String getPassword();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.druid.metadata;

import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

public class DefaultPasswordProviderTest
{
private static final String pwd = "nothing";
private static final ObjectMapper jsonMapper = new ObjectMapper();

@Test
public void testExplicitConstruction() {
DefaultPasswordProvider pp = new DefaultPasswordProvider(pwd);
Assert.assertEquals(pwd, pp.getPassword());
}

@Test
public void testFromStringConstruction() {
DefaultPasswordProvider pp = DefaultPasswordProvider.fromString(pwd);
Assert.assertEquals(pwd, pp.getPassword());
}

@Test
public void testDeserializationFromJsonString() throws Exception {
PasswordProvider pp = jsonMapper.readValue("\"" + pwd + "\"",
PasswordProvider.class);
Assert.assertEquals(pwd, pp.getPassword());
}

@Test
public void testDeserializationFromJson() throws Exception {
PasswordProvider pp = jsonMapper.readValue(
"{\"type\": \"default\", \"password\": \"" + pwd + "\"}",
PasswordProvider.class);
Assert.assertEquals(pwd, pp.getPassword());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.druid.metadata;

import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

public class MetadataStorageConnectorConfigTest {

private static final ObjectMapper jsonMapper = new ObjectMapper();

@Test
public void testMetadaStorageConnectionConfigSimplePassword() throws Exception
{
testMetadataStorageConnectionConfig(
true,
"host",
1234,
"connectURI",
"user",
"\"nothing\"",
"nothing");
}

@Test
public void testMetadaStorageConnectionConfigWithDefaultProviderPassword() throws Exception
{
testMetadataStorageConnectionConfig(
true,
"host",
1234,
"connectURI",
"user",
"{\"type\":\"default\",\"password\":\"nothing\"}",
"nothing");
}

private void testMetadataStorageConnectionConfig(
boolean createTables,
String host,
int port,
String connectURI,
String user,
String pwdString,
String pwd
) throws Exception
{
MetadataStorageConnectorConfig config = jsonMapper.readValue(
"{" +
"\"createTables\": \"" + createTables + "\"," +
"\"host\": \"" + host + "\"," +
"\"port\": \"" + port + "\"," +
"\"connectURI\": \"" + connectURI + "\"," +
"\"user\": \"" + user + "\"," +
"\"password\": " + pwdString +
"}",
MetadataStorageConnectorConfig.class);

Assert.assertEquals(host, config.getHost());
Assert.assertEquals(port, config.getPort());
Assert.assertEquals(connectURI, config.getConnectURI());
Assert.assertEquals(user, config.getUser());
Assert.assertEquals(pwd, config.getPassword());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Supplier;
import io.druid.metadata.MetadataStorageConnectorConfig;
import io.druid.metadata.PasswordProvider;

import javax.validation.constraints.NotNull;

Expand All @@ -38,7 +39,7 @@ public class MetadataStorageUpdaterJobSpec implements Supplier<MetadataStorageCo
public String user;

@JsonProperty("password")
public String password;
private PasswordProvider passwordProvider;

@JsonProperty("segmentTable")
public String segmentTable;
Expand Down Expand Up @@ -73,7 +74,7 @@ public String getUser()
@Override
public String getPassword()
{
return password;
return passwordProvider == null ? null : passwordProvider.getPassword();
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.druid.indexer.updater;

import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

public class MetadataStorageUpdaterJobSpecTest
{
private static final ObjectMapper jsonMapper = new ObjectMapper();

@Test
public void testMetadaStorageConnectionConfigSimplePassword() throws Exception
{
testMetadataStorageUpdaterJobSpec(
"segments_table",
"db",
"jdbc:mysql://localhost/druid",
"druid",
"\"nothing\"",
"nothing");
}

@Test
public void testMetadaStorageConnectionConfigWithDefaultProviderPassword() throws Exception
{
testMetadataStorageUpdaterJobSpec(
"segments_table",
"db",
"jdbc:mysql://localhost/druid",
"druid",
"{\"type\":\"default\",\"password\":\"nothing\"}",
"nothing");
}

private void testMetadataStorageUpdaterJobSpec(
String segmentTable,
String type,
String connectURI,
String user,
String pwdString,
String pwd
) throws Exception
{
MetadataStorageUpdaterJobSpec spec = jsonMapper.readValue(
"{" +
"\"type\": \"" + type + "\",\n" +
"\"connectURI\": \"" + connectURI + "\",\n" +
"\"user\": \"" + user + "\",\n" +
"\"password\": " + pwdString + ",\n" +
"\"segmentTable\": \"" + segmentTable + "\"\n" +
"}",
MetadataStorageUpdaterJobSpec.class);

Assert.assertEquals(segmentTable, spec.getSegmentTable());
Assert.assertEquals(type, spec.getType());
Assert.assertEquals("jdbc:mysql://localhost/druid", spec.get().getConnectURI());
Assert.assertEquals(user, spec.get().getUser());
Assert.assertEquals(pwd, spec.get().getPassword());
}
}

0 comments on commit 126262e

Please sign in to comment.