Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e3e2f96161fc2e215300817169461bd0f74684b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*******************************************************************************
 * Copyright (c) 2008, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.security.ui.storage.view;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.equinox.internal.security.ui.Activator;
import org.eclipse.equinox.internal.security.ui.nls.SecUIMessages;
import org.eclipse.equinox.internal.security.ui.storage.IStorageConst;
import org.eclipse.equinox.security.storage.ISecurePreferences;
import org.eclipse.equinox.security.storage.StorageException;
import org.eclipse.jface.action.*;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.*;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;

/**
 * Table of { key -> value } pairs associated with the node.
 */
public class ValuesView {

	/**
	 * The default value of this variable (false, meaning "not in debug of secure storage"
	 * removes showValueAction, encryptValueAction, and decryptValueAction.
	 */
	static private boolean inDevelopmentMode = false;

	/**
	 * Line to show for encrypted values
	 */
	private final static String ENCRYPTED_SUBSTITUTE = "**********"; //$NON-NLS-1$

	protected ISecurePreferencesSelection parentView;
	protected TableViewer tableViewer;

	protected ISecurePreferences selectedNode = null;

	protected Action addValueAction;
	protected Action removeValueAction;
	protected Action showValueAction = null;
	protected Action encryptValueAction = null;
	protected Action decryptValueAction = null;

	protected Shell shell;

	class TableValuesElement {
		private String key;
		private String value;
		private boolean encrypted;

		public TableValuesElement(String key) {
			this.key = key;
			this.value = null;
			encrypted = true;
		}

		public TableValuesElement(String key, String value) {
			this.key = key;
			this.value = value;
			encrypted = false;
		}

		public String getKey() {
			return key;
		}

		public void setValue(String value) {
			this.value = value;
		}

		public String getValue() {
			if (value == null)
				return ENCRYPTED_SUBSTITUTE;
			return value;
		}

		public boolean isEncrypted() {
			return encrypted;
		}
	}

	class TableContentProvider implements IStructuredContentProvider {

		@Override
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
			selectedNode = (ISecurePreferences) newInput;
		}

		@Override
		public void dispose() {
			// nothing to do
		}

		@Override
		public Object[] getElements(Object parent) {
			if (selectedNode == null)
				return new Object[0];
			String[] keys = selectedNode.keys();
			TableValuesElement[] result = new TableValuesElement[keys.length];
			for (int i = 0; i < keys.length; i++) {
				try {
					if (selectedNode.isEncrypted(keys[i]))
						result[i] = new TableValuesElement(keys[i]);
					else
						result[i] = new TableValuesElement(keys[i], selectedNode.get(keys[i], null));
				} catch (StorageException e) {
					Activator.log(IStatus.ERROR, SecUIMessages.failedDecrypt, null, e);
				}
			}
			return result;
		}
	}

	class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
		@Override
		public String getColumnText(Object obj, int index) {
			if (obj == null)
				return null;
			if (!(obj instanceof TableValuesElement))
				return obj.toString();
			switch (index) {
				case 0 :
					return ((TableValuesElement) obj).getKey();
				case 1 :
					return ((TableValuesElement) obj).getValue();
				default :
					return obj.toString();
			}
		}

		@Override
		public String getText(Object element) {
			return getColumnText(element, 0);
		}

		@Override
		public Image getColumnImage(Object obj, int index) {
			return null;
		}

		@Override
		public Image getImage(Object obj) {
			return null;
		}
	}

	public ValuesView(Table table, final ISecurePreferencesSelection parentView, Shell shell) {
		this.parentView = parentView;
		this.shell = shell;

		TableColumn keysColumn = new TableColumn(table, SWT.LEFT);
		keysColumn.setText(SecUIMessages.keysColumn);
		TableColumn valuesColumn = new TableColumn(table, SWT.LEFT);
		valuesColumn.setText(SecUIMessages.valuesColumn);

		TableLayout layout = new TableLayout();
		layout.addColumnData(new ColumnWeightData(1));
		layout.addColumnData(new ColumnWeightData(2));
		table.setLayout(layout);

		tableViewer = new TableViewer(table);

		tableViewer.setContentProvider(new TableContentProvider());
		tableViewer.setLabelProvider(new TableLabelProvider());
		tableViewer.setComparator(new ViewerComparator());

		if (Activator.getDefault().debugStorageContents()) {
			makeActions();
			hookContextMenu();
		}
	}

	private void hookContextMenu() {
		MenuManager menuMgr = new MenuManager(SecUIMessages.nodesContextMenu);

		menuMgr.addMenuListener(manager -> {
			if (selectedNode == null) {
				addValueAction.setEnabled(false);
				removeValueAction.setEnabled(false);
				return;
			}
			boolean isInternal = selectedNode.absolutePath().startsWith(IStorageConst.PROVIDER_NODE);
			addValueAction.setEnabled(!isInternal);
			removeValueAction.setEnabled(!isInternal);
			if (encryptValueAction != null)
				encryptValueAction.setEnabled(!isInternal);
			if (decryptValueAction != null)
				decryptValueAction.setEnabled(!isInternal);
			if (showValueAction != null)
				showValueAction.setEnabled(false);

			// enablement of encrypted/decrypted
			StructuredSelection selection = (StructuredSelection) tableViewer.getSelection();
			Object selected = selection.getFirstElement();
			if (selected instanceof TableValuesElement) {
				String key = ((TableValuesElement) selected).getKey();
				try {
					boolean encrypted = selectedNode.isEncrypted(key);
					if (encryptValueAction != null)
						encryptValueAction.setEnabled(!isInternal && !encrypted);
					if (decryptValueAction != null)
						decryptValueAction.setEnabled(!isInternal && encrypted);
					if (showValueAction != null)
						showValueAction.setEnabled(encrypted);
				} catch (StorageException e) {
					Activator.log(IStatus.ERROR, SecUIMessages.failedDecrypt, null, e);
				}
			}
		});
		Menu menu = menuMgr.createContextMenu(tableViewer.getControl());
		tableViewer.getControl().setMenu(menu);

		// fill context menu
		menuMgr.add(addValueAction);
		menuMgr.add(removeValueAction);
		if (showValueAction != null) {
			menuMgr.add(new Separator());
			menuMgr.add(showValueAction);
		}
		if (encryptValueAction != null) {
			menuMgr.add(new Separator());
			menuMgr.add(encryptValueAction);
		}
		if (decryptValueAction != null)
			menuMgr.add(decryptValueAction);
	}

	private void makeActions() {
		addValueAction = new Action() {
			@Override
			public void run() {
				if (selectedNode == null)
					return;

				NewValueDialog newValueDialog = new NewValueDialog(selectedNode.keys(), tableViewer.getControl().getShell());
				if (newValueDialog.open() != Window.OK)
					return;
				String key = newValueDialog.getKey();
				String value = newValueDialog.getValue();
				boolean encrypt = newValueDialog.encrypt();
				try {
					selectedNode.put(key, value, encrypt);
					parentView.modified();
				} catch (StorageException e) {
					Activator.log(IStatus.ERROR, SecUIMessages.failedEncrypt, null, e);
				}
				tableViewer.refresh();
			}
		};
		addValueAction.setText(SecUIMessages.addValueCommand);
		addValueAction.setToolTipText(SecUIMessages.addValueCommandTmp);
		addValueAction.setImageDescriptor(ImageDescriptor.createFromFile(NodesView.class, "/icons/storage/value_new.gif")); //$NON-NLS-1$

		removeValueAction = new Action() {
			@Override
			public void run() {
				if (selectedNode == null)
					return;
				StructuredSelection selection = (StructuredSelection) tableViewer.getSelection();
				Object selected = selection.getFirstElement();
				if (!(selected instanceof TableValuesElement))
					return;

				TableValuesElement node = (TableValuesElement) selected;
				String key = node.getKey();

				// "Are you sure?" dialog 
				MessageBox dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.YES | SWT.NO);
				dialog.setText(SecUIMessages.removeValueTitle);
				String msg = NLS.bind(SecUIMessages.removeValueMsg, key);
				dialog.setMessage(msg);
				if (dialog.open() != SWT.YES)
					return;
				selectedNode.remove(key);
				parentView.modified();
				tableViewer.refresh();
			}
		};
		removeValueAction.setText(SecUIMessages.removeValueCommand);
		removeValueAction.setToolTipText(SecUIMessages.removeValueCommandTmp);
		removeValueAction.setImageDescriptor(ImageDescriptor.createFromFile(NodesView.class, "/icons/storage/value_delete.gif")); //$NON-NLS-1$

		if (inDevelopmentMode)
			addDevelopmentMenuOptions();
	}

	private void addDevelopmentMenuOptions() {
		showValueAction = new Action() {
			@Override
			public void run() {
				if (selectedNode == null)
					return;
				StructuredSelection selection = (StructuredSelection) tableViewer.getSelection();
				Object selected = selection.getFirstElement();
				if (!(selected instanceof TableValuesElement))
					return;

				TableValuesElement node = (TableValuesElement) selected;
				String key = node.getKey();
				try {
					String value = selectedNode.get(key, null);
					MessageBox dialog = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
					dialog.setText(SecUIMessages.showValueTitle);
					String msg = NLS.bind(SecUIMessages.showValueMsg, key, value);
					dialog.setMessage(msg);
					dialog.open();
				} catch (StorageException e) {
					Activator.log(IStatus.ERROR, SecUIMessages.failedDecrypt, null, e);
					MessageBox dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
					dialog.setMessage(SecUIMessages.failedDecrypt);
					dialog.open();
					return;
				}
			}
		};
		showValueAction.setText(SecUIMessages.showValueCommand);
		showValueAction.setToolTipText(SecUIMessages.showValueCommandTmp);
		showValueAction.setImageDescriptor(ImageDescriptor.createFromFile(NodesView.class, "/icons/storage/value_show.gif")); //$NON-NLS-1$

		encryptValueAction = new Action() {
			@Override
			public void run() {
				reCodeValue(true);
			}
		};
		encryptValueAction.setText(SecUIMessages.encryptValueCommand);
		encryptValueAction.setToolTipText(SecUIMessages.encryptValueCommandTmp);
		encryptValueAction.setImageDescriptor(ImageDescriptor.createFromFile(NodesView.class, "/icons/storage/encrypt.gif")); //$NON-NLS-1$

		decryptValueAction = new Action() {
			@Override
			public void run() {
				reCodeValue(false);
			}
		};
		decryptValueAction.setText(SecUIMessages.decryptValueCommand);
		decryptValueAction.setToolTipText(SecUIMessages.decryptValueCommandTmp);
		decryptValueAction.setImageDescriptor(ImageDescriptor.createFromFile(NodesView.class, "/icons/storage/decrypt.gif")); //$NON-NLS-1$
	}

	protected void reCodeValue(boolean encrypted) {
		if (selectedNode == null)
			return;
		StructuredSelection selection = (StructuredSelection) tableViewer.getSelection();
		Object selected = selection.getFirstElement();
		if (!(selected instanceof TableValuesElement))
			return;

		TableValuesElement node = (TableValuesElement) selected;
		String key = node.getKey();
		String value;
		try {
			value = selectedNode.get(key, null);
		} catch (StorageException e) {
			MessageBox dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
			dialog.setMessage(SecUIMessages.failedDecrypt);
			dialog.open();
			Activator.log(IStatus.ERROR, SecUIMessages.failedDecrypt, null, e);
			tableViewer.refresh();
			return;
		}
		try {
			selectedNode.put(key, value, encrypted);
			parentView.modified();
		} catch (StorageException e) {
			MessageBox dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
			dialog.setMessage(SecUIMessages.failedEncrypt);
			dialog.open();
			Activator.log(IStatus.ERROR, SecUIMessages.failedEncrypt, null, e);
			tableViewer.refresh();
			return;
		}
		tableViewer.refresh();
	}

	public void setInput(Object input) {
		tableViewer.setInput(input);
	}

}

Back to the top