1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tradefed.util.keystore; 18 19 import com.android.tradefed.log.LogUtil.CLog; 20 import com.android.tradefed.util.FileUtil; 21 22 import org.json.JSONException; 23 import org.json.JSONObject; 24 25 import java.io.File; 26 import java.io.IOException; 27 28 /** 29 * A sample implementation where a local JSON file acts a key store. The JSON 30 * text file should have key to value in string format. 31 */ 32 public class JSONFileKeyStoreClient implements IKeyStoreClient { 33 34 private File mJsonFile = null; 35 // JSON key store read from the JSON file. 36 protected JSONObject mJsonKeyStore = null; 37 JSONFileKeyStoreClient()38 public JSONFileKeyStoreClient() { 39 } 40 JSONFileKeyStoreClient(File jsonFile)41 public JSONFileKeyStoreClient(File jsonFile) throws KeyStoreException { 42 mJsonFile = jsonFile; 43 if (mJsonFile == null) { 44 throw new KeyStoreException("JSON key store file not set."); 45 } 46 if (!mJsonFile.canRead()) { 47 throw new KeyStoreException( 48 String.format("Unable to read the JSON key store file %s", 49 mJsonFile.toString())); 50 } 51 try { 52 String data = FileUtil.readStringFromFile(mJsonFile); 53 mJsonKeyStore = new JSONObject(data); 54 } catch (IOException e) { 55 throw new KeyStoreException(String.format("Failed to read JSON key file %s: %s", 56 mJsonFile.toString(), e)); 57 } catch (JSONException e) { 58 throw new KeyStoreException( 59 String.format("Failed to parse JSON data from file %s with exception: %s", 60 mJsonFile.toString(), e)); 61 } 62 63 } 64 65 @Override isAvailable()66 public boolean isAvailable() { 67 return mJsonKeyStore != null; 68 } 69 70 @Override containsKey(String key)71 public boolean containsKey(String key) { 72 CLog.i("fetching key for %s", key); 73 if (mJsonKeyStore == null) { 74 CLog.w("Key Store is null"); 75 return false; 76 } 77 return mJsonKeyStore.has(key); 78 } 79 80 @Override fetchKey(String key)81 public String fetchKey(String key) { 82 if (key == null) { 83 CLog.w("null key passed"); 84 return null; 85 } 86 if (mJsonKeyStore == null) { 87 CLog.w("null keystore"); 88 return null; 89 } 90 try { 91 return mJsonKeyStore.getString(key); 92 } catch (JSONException e) { 93 CLog.e("failed to fetch key from json key store"); 94 CLog.e(e); 95 return null; 96 } 97 } 98 99 /** 100 * Helper method used to set key store. Used for testing. 101 * 102 * @param keyStore {@link JSONObject} to use as key store. 103 */ setKeyStore(JSONObject keyStore)104 public void setKeyStore(JSONObject keyStore) { 105 mJsonKeyStore = keyStore; 106 } 107 108 /** 109 * Maps {@code key} to {@code value}. Equivalent to {@code mJsonKeyStore.put(key, value)}. 110 * 111 * @param the key to set value. 112 * @param the {@link String} value of the key. 113 */ setKey(String key, String value)114 protected void setKey(String key, String value) throws JSONException { 115 if (key == null) { 116 CLog.w("null key passed"); 117 return; 118 } 119 if (mJsonKeyStore == null) { 120 CLog.w("null keystore"); 121 return; 122 } 123 mJsonKeyStore.put(key, value); 124 } 125 } 126