Search in sources :

Example 1 with SecureStore

use of org.phoebus.security.store.SecureStore in project phoebus by ControlSystemStudio.

the class SecureStoreTest method init.

@BeforeClass
public static void init() throws Exception {
    File secureStoreFile;
    secureStoreFile = new File(System.getProperty("user.home"), "TestOnlySecureStore.dat");
    secureStoreFile.deleteOnExit();
    String password = "forTestPurposesOnly";
    secureStore = new SecureStore(secureStoreFile, password.toCharArray());
}
Also used : File(java.io.File) SecureStore(org.phoebus.security.store.SecureStore) BeforeClass(org.junit.BeforeClass)

Example 2 with SecureStore

use of org.phoebus.security.store.SecureStore in project phoebus by ControlSystemStudio.

the class FieldsViewController method fetchStoredUserCredentials.

public void fetchStoredUserCredentials() {
    // Perform file IO on background thread.
    JobManager.schedule("Access Secure Store", monitor -> {
        // Get the SecureStore. Retrieve username and password.
        try {
            SecureStore store = new SecureStore();
            ScopedAuthenticationToken scopedAuthenticationToken = store.getScopedAuthenticationToken(LogService.AUTHENTICATION_SCOPE);
            // Could be accessed from JavaFX Application Thread when updating, so synchronize.
            synchronized (usernameProperty) {
                usernameProperty.set(scopedAuthenticationToken == null ? "" : scopedAuthenticationToken.getUsername());
            }
            synchronized (passwordProperty) {
                passwordProperty.set(scopedAuthenticationToken == null ? "" : scopedAuthenticationToken.getPassword());
            }
            // Let anyone listening know that their credentials are now out of date.
            updateCredentials.set(true);
        // checkIfReadyToSubmit();
        } catch (Exception ex) {
            logger.log(Level.WARNING, "Secure Store file not found.", ex);
        }
    });
}
Also used : ScopedAuthenticationToken(org.phoebus.security.tokens.ScopedAuthenticationToken) SecureStore(org.phoebus.security.store.SecureStore)

Example 3 with SecureStore

use of org.phoebus.security.store.SecureStore in project phoebus by ControlSystemStudio.

the class LogEntryEditorController method submit.

@FXML
public void submit() {
    submissionInProgress.set(true);
    JobManager.schedule("Submit Log Entry", monitor -> {
        OlogLog ologLog = new OlogLog();
        ologLog.setTitle(fieldsViewController.getTitle());
        ologLog.setDescription(fieldsViewController.getDescription());
        ologLog.setLevel(fieldsViewController.getSelectedLevel());
        ologLog.setLogbooks(fieldsViewController.getSelectedLogbooks());
        ologLog.setTags(fieldsViewController.getSelectedTags());
        ologLog.setAttachments(attachmentsViewController.getAttachments());
        ologLog.setProperties(logPropertiesEditorController.getProperties());
        LogClient logClient = logFactory.getLogClient(new SimpleAuthenticationToken(fieldsViewController.getUsernameProperty(), fieldsViewController.getPasswordProperty()));
        LogEntry result;
        try {
            if (replyTo == null) {
                result = logClient.set(ologLog);
            } else {
                result = logClient.reply(ologLog, replyTo);
            }
            if (result != null) {
                if (completionHandler != null) {
                    completionHandler.handleResult(result);
                }
                // Set username and password in secure store if submission of log entry completes successfully
                if (LogbookUIPreferences.save_credentials) {
                    // Get the SecureStore. Store username and password.
                    try {
                        SecureStore store = new SecureStore();
                        ScopedAuthenticationToken scopedAuthenticationToken = new ScopedAuthenticationToken(LogService.AUTHENTICATION_SCOPE, fieldsViewController.getUsernameProperty(), fieldsViewController.getPasswordProperty());
                        store.setScopedAuthentication(scopedAuthenticationToken);
                    } catch (Exception ex) {
                        logger.log(Level.WARNING, "Secure Store file not found.", ex);
                    }
                }
                attachmentsViewController.deleteTemporaryFiles();
                // This will close the editor
                Platform.runLater(() -> cancel());
            }
        } catch (LogbookException e) {
            logger.log(Level.WARNING, "Unable to submit log entry", e);
            Platform.runLater(() -> {
                if (e.getCause() != null && e.getCause().getMessage() != null) {
                    completionMessageLabel.textProperty().setValue(e.getCause().getMessage());
                } else if (e.getMessage() != null) {
                    completionMessageLabel.textProperty().setValue(e.getMessage());
                } else {
                    completionMessageLabel.textProperty().setValue(org.phoebus.logbook.Messages.SubmissionFailed);
                }
            });
        }
        submissionInProgress.set(false);
    });
}
Also used : LogClient(org.phoebus.logbook.LogClient) ScopedAuthenticationToken(org.phoebus.security.tokens.ScopedAuthenticationToken) LogbookException(org.phoebus.logbook.LogbookException) OlogLog(org.phoebus.olog.es.api.model.OlogLog) SecureStore(org.phoebus.security.store.SecureStore) LogEntry(org.phoebus.logbook.LogEntry) LogbookException(org.phoebus.logbook.LogbookException) ExecutionException(java.util.concurrent.ExecutionException) SimpleAuthenticationToken(org.phoebus.security.tokens.SimpleAuthenticationToken) FXML(javafx.fxml.FXML)

Example 4 with SecureStore

use of org.phoebus.security.store.SecureStore in project phoebus by ControlSystemStudio.

the class LogEntryModel method submitEntry.

/**
 * Create and return a log entry with the current data in the log entry form.
 * NOTE: this method calls the remote service in a synchronous manner. Calling code should handle potential
 * threading issues, e.g. invoking this method on the UI thread. Using a synchronous approach facilitates
 * handling of connection or authentication issues.
 *
 * @throws IOException
 */
public LogEntry submitEntry() throws Exception {
    // Create a log entry with the form data.
    LogEntryBuilder logEntryBuilder = new LogEntryBuilder();
    logEntryBuilder.title(title).description(text).level(level);
    for (String selectedLogbook : selectedLogbooks) logEntryBuilder.appendToLogbook(LogbookImpl.of(selectedLogbook));
    for (String selectedTag : selectedTags) logEntryBuilder.appendTag(TagImpl.of(selectedTag));
    // List of temporary image files to delete.
    List<File> toDelete = new ArrayList<>();
    // Add Images
    for (Image image : images) {
        File imageFile = File.createTempFile("log_entry_image_", ".png");
        imageFile.deleteOnExit();
        toDelete.add(imageFile);
        ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", imageFile);
        logEntryBuilder.attach(AttachmentImpl.of(imageFile, "image", false));
    }
    // Add Files
    for (File file : files) {
        logEntryBuilder.attach(AttachmentImpl.of(file, "file", false));
    }
    LogEntry logEntry = logEntryBuilder.build();
    if (LogbookUiPreferences.save_credentials) {
        // Get the SecureStore. Store username and password.
        try {
            SecureStore store = new SecureStore();
            store.set(SecureStore.USERNAME_TAG, username);
            store.set(SecureStore.PASSWORD_TAG, password);
        } catch (Exception ex) {
            logger.log(Level.WARNING, "Secure Store file not found.", ex);
        }
    }
    LogEntry result = null;
    if (null != logFactory)
        result = logFactory.getLogClient(new SimpleAuthenticationToken(username, password)).set(logEntry);
    // Delete the temporary files.
    for (File file : toDelete) file.delete();
    // Run the onSubmitAction runnable
    if (null != onSubmitAction)
        onSubmitAction.run();
    return result;
}
Also used : ArrayList(java.util.ArrayList) LogEntryBuilder(org.phoebus.logbook.LogEntryImpl.LogEntryBuilder) Image(javafx.scene.image.Image) File(java.io.File) SecureStore(org.phoebus.security.store.SecureStore) LogEntry(org.phoebus.logbook.LogEntry) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SimpleAuthenticationToken(org.phoebus.security.tokens.SimpleAuthenticationToken)

Example 5 with SecureStore

use of org.phoebus.security.store.SecureStore in project phoebus by ControlSystemStudio.

the class CredentialsManagementApp method create.

@Override
public AppInstance create() {
    List<ServiceAuthenticationProvider> authenticationProviders = ServiceLoader.load(ServiceAuthenticationProvider.class).stream().map(Provider::get).collect(Collectors.toList());
    try {
        SecureStore secureStore = new SecureStore();
        new CredentialsManagementStage(authenticationProviders, secureStore).show();
    } catch (Exception e) {
        ExceptionDetailsErrorDialog.openError(Messages.SecureStoreErrorTitle, Messages.SecureStoreErrorBody, e);
    }
    return null;
}
Also used : SecureStore(org.phoebus.security.store.SecureStore) ServiceAuthenticationProvider(org.phoebus.security.authorization.ServiceAuthenticationProvider)

Aggregations

SecureStore (org.phoebus.security.store.SecureStore)6 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 LogEntry (org.phoebus.logbook.LogEntry)2 ScopedAuthenticationToken (org.phoebus.security.tokens.ScopedAuthenticationToken)2 SimpleAuthenticationToken (org.phoebus.security.tokens.SimpleAuthenticationToken)2 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 FXML (javafx.fxml.FXML)1 Image (javafx.scene.image.Image)1 BeforeClass (org.junit.BeforeClass)1 LogClient (org.phoebus.logbook.LogClient)1 LogEntryBuilder (org.phoebus.logbook.LogEntryImpl.LogEntryBuilder)1 LogbookException (org.phoebus.logbook.LogbookException)1 OlogLog (org.phoebus.olog.es.api.model.OlogLog)1 ServiceAuthenticationProvider (org.phoebus.security.authorization.ServiceAuthenticationProvider)1