Persist credentials depending on conf setting

This commit is contained in:
Prasad Wagle 2016-05-10 15:02:05 -07:00
parent d9dae049da
commit 8b70f0894d
4 changed files with 117 additions and 3 deletions

View file

@ -18,6 +18,8 @@
package org.apache.zeppelin.user;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -33,15 +35,30 @@ public class Credentials {
private static Credentials credentials = null;
private Map<String, UserCredentials> credentialsMap;
private Gson gson;
private Boolean credentialsPersist = true;
File credentialsFile;
private Credentials() {
private Credentials(Boolean credentialsPersist, String credentialsPath) {
this.credentialsPersist = credentialsPersist;
credentialsFile = new File(credentialsPath);
credentialsMap = new HashMap<>();
if (credentialsPersist) {
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
gson = builder.create();
loadFromFile();
}
}
public static synchronized Credentials getCredentials() {
public static synchronized void initCredentials(Boolean credentialsPersist,
String credentialsPath) {
if (credentials == null) {
credentials = new Credentials();
credentials = new Credentials(credentialsPersist, credentialsPath);
}
}
public static Credentials getCredentials() {
return credentials;
}
@ -55,6 +72,62 @@ public class Credentials {
public void putUserCredentials(String username, UserCredentials uc) throws IOException {
credentialsMap.put(username, uc);
if (credentialsPersist) {
saveToFile();
}
}
private void loadFromFile() {
LOG.info(credentialsFile.getAbsolutePath());
if (!credentialsFile.exists()) {
// nothing to read
return;
}
try {
FileInputStream fis = new FileInputStream(credentialsFile);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
isr.close();
fis.close();
String json = sb.toString();
CredentialsInfoSaving info = gson.fromJson(json, CredentialsInfoSaving.class);
this.credentialsMap = info.credentialsMap;
} catch (IOException e) {
LOG.error("Error loading credentials file");
e.printStackTrace();
}
}
private void saveToFile() throws IOException {
String jsonString;
synchronized (credentials) {
CredentialsInfoSaving info = new CredentialsInfoSaving();
info.credentialsMap = credentialsMap;
jsonString = gson.toJson(info);
}
try {
if (!credentialsFile.exists()) {
credentialsFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(credentialsFile, false);
OutputStreamWriter out = new OutputStreamWriter(fos);
out.append(jsonString);
out.close();
fos.close();
} catch (IOException e) {
LOG.error("Error saving credentials file");
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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 org.apache.zeppelin.user;
import java.util.Map;
/**
* Helper class to save credentials
*/
public class CredentialsInfoSaving {
public Map<String, UserCredentials> credentialsMap;
}

View file

@ -31,6 +31,7 @@ import org.apache.zeppelin.scheduler.SchedulerFactory;
import org.apache.zeppelin.search.LuceneSearch;
import org.apache.zeppelin.search.SearchService;
import org.apache.zeppelin.socket.NotebookServer;
import org.apache.zeppelin.user.Credentials;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
@ -90,6 +91,9 @@ public class ZeppelinServer extends Application {
ZeppelinConfiguration conf = ZeppelinConfiguration.create();
conf.setProperty("args", args);
Credentials.initCredentials(conf.credentialsPersist(),
conf.getCredentialsPath());
jettyWebServer = setupJettyServer(conf);
ContextHandlerCollection contexts = new ContextHandlerCollection();

View file

@ -362,6 +362,14 @@ public class ZeppelinConfiguration extends XMLConfiguration {
return getRelativeDir(String.format("%s/notebook-authorization.json", getConfDir()));
}
public Boolean credentialsPersist() {
return getBoolean(ConfVars.ZEPPELIN_CREDENTIALS_PERSIST);
}
public String getCredentialsPath() {
return getRelativeDir(String.format("%s/credentials.json", getConfDir()));
}
public String getShiroPath() {
return getRelativeDir(String.format("%s/shiro.ini", getConfDir()));
}
@ -528,8 +536,10 @@ public class ZeppelinConfiguration extends XMLConfiguration {
// i.e. http://localhost:8080
ZEPPELIN_ALLOWED_ORIGINS("zeppelin.server.allowed.origins", "*"),
ZEPPELIN_ANONYMOUS_ALLOWED("zeppelin.anonymous.allowed", true),
ZEPPELIN_CREDENTIALS_PERSIST("zeppelin.credentials.persist", true),
ZEPPELIN_WEBSOCKET_MAX_TEXT_MESSAGE_SIZE("zeppelin.websocket.max.text.message.size", "1024000");
private String varName;
@SuppressWarnings("rawtypes")
private Class varClass;