Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions services/save-and-restore/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,8 @@ The response is a snapshot object representing the persisted data, see :ref:`Get
Server Restore Endpoints
------------------------

**NOTE:** the restore endpoints require an authenticated user to have role ``sar-server-restore``.

Restore from snapshot items
"""""""""""""""""""""""""""

Expand Down Expand Up @@ -1042,10 +1044,10 @@ service can be configured to delegate authentication to Active Directory or remo
purposes hard coded credentials are found in the ``WebSecurityConfig`` class. See the file ``application.properties``
for information on how to select authentication method.

Two roles are defined, "sar-user" and "sar-admin". The actual name of these roles can be customizable in ``application.properties``,
Three roles are defined, "sar-user", "sar-admin" and "sar-server-restore". The actual name of these roles can be customizable in ``application.properties``,
and must match role/group names in LDAP or Active Directory.

Authorization uses a role-based approach like so:
The role-based authorization works like so:

* Unauthenticated users may read data, i.e. access GET endpoints.
* Save-and-restore role "sar-user":
Expand All @@ -1060,7 +1062,8 @@ Authorization uses a role-based approach like so:
* Object is configuration or folder node with no child nodes
* Object is a filter
* Object is a tag
* Save-and-restore role "sar-admin": no restrictions
* Save-and-restore role "sar-admin": no restrictions, except for server-side restore
* Role "sar-server-restore" is used only to control access to endpoints related to restore operations.

Authentication endpoint
-----------------------
Expand All @@ -1077,11 +1080,30 @@ Body:

{"username":"johndoe", "password":"undisclosed"}

Handles client authentication. Upon successful authentication it returns an object like so:

.. code-block:: JSON

{
"userName": "johndoe",
"roles" : ["role1", "role2"]
}


**.../server-restore-authorized**

Method: POST

Returns ``true`` if an authenticated client may request the service to perform a restore operation, otherwise
``false``. Note though that calling this endpoint from an unauthenticated client will return a HTTP 401 response.



Enabled authentication, disabled authorization
----------------------------------------------

The application property ``authorization.permitall`` (default ``true``) can be used to bypass all authorization. In
The application property ``authorization.permitall`` (default ``true``) can be used to bypass all authorization, except
for endpoints related to snapshot restore on the service. In
this case authentication is still required for protected endpoints, but user need not be associated with
a save-and-restore role/group.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2026 European Spallation Source ERIC.
*/

package org.phoebus.service.saveandrestore.persistence.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* Component to centralize values related to Elasticsearch index names.
*/
@Component
public class IndexConfig {

@SuppressWarnings("unused")
@Value("${elasticsearch.tree_node.index:saveandrestore_tree}")
public String ES_TREE_INDEX;

Check warning on line 18 in services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/persistence/config/IndexConfig.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "ES_TREE_INDEX" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHple_LI8Q0yZdRnofz&open=AaHple_LI8Q0yZdRnofz&pullRequest=4028

@SuppressWarnings("unused")
@Value("${elasticsearch.configuration_node.index:saveandrestore_configuration}")
public String ES_CONFIGURATION_INDEX;

Check warning on line 22 in services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/persistence/config/IndexConfig.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "ES_CONFIGURATION_INDEX" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHple_LI8Q0yZdRnof0&open=AaHple_LI8Q0yZdRnof0&pullRequest=4028

@SuppressWarnings("unused")
@Value("${elasticsearch.snapshot_node.index:saveandrestore_snapshot}")
public String ES_SNAPSHOT_INDEX;

Check warning on line 26 in services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/persistence/config/IndexConfig.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "ES_SNAPSHOT_INDEX" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHple_LI8Q0yZdRnof1&open=AaHple_LI8Q0yZdRnof1&pullRequest=4028

@SuppressWarnings("unused")
@Value("${elasticsearch.composite_snapshot_node.index:saveandrestore_composite_snapshot}")
public String ES_COMPOSITE_SNAPSHOT_INDEX;

Check warning on line 30 in services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/persistence/config/IndexConfig.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "ES_COMPOSITE_SNAPSHOT_INDEX" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHple_LI8Q0yZdRnof2&open=AaHple_LI8Q0yZdRnof2&pullRequest=4028

@SuppressWarnings("unused")
@Value("${elasticsearch.filter.index:saveandrestore_filter}")
public String ES_FILTER_INDEX;

Check warning on line 34 in services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/persistence/config/IndexConfig.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "ES_FILTER_INDEX" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHple_LI8Q0yZdRnof3&open=AaHple_LI8Q0yZdRnof3&pullRequest=4028
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,21 @@
@Value("${demo.readOnly.password:1234}")
private String demoReadOnlyPassword;

@Value("${demo.server.restore.user:serverRestoreUser}")
private String demoServerRestoreUser;

@Value("${demo.server.restore.password:serverRestoreUserPass}")
private String demoServerRestoreUserPass;

@Autowired
private String roleUser;

@Autowired
private String roleAdmin;

@Autowired

Check warning on line 63 in services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/config/InMemorySecurityConfig.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this field injection and use constructor injection instead.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHple-QI8Q0yZdRnofv&open=AaHple-QI8Q0yZdRnofv&pullRequest=4028
private String roleServerRestore;

Check failure on line 64 in services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/config/InMemorySecurityConfig.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Inject this field value directly into "userDetailsService", the only method that uses it.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHple-QI8Q0yZdRnofw&open=AaHple-QI8Q0yZdRnofw&pullRequest=4028

@Bean(WebSecurityConfig.IN_MEMORY)
public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(userDetailsService());
Expand All @@ -78,7 +87,12 @@
.username(demoReadOnly)
.password(passwordEncoder.encode(demoReadOnlyPassword))
.build();
return new InMemoryUserDetailsManager(user, adminUser, readOnlyUser);
UserDetails serverRestoreUser = User.builder()
.username(demoServerRestoreUser)
.password(passwordEncoder.encode(demoServerRestoreUserPass))
.roles(roleServerRestore.toUpperCase())
.build();
return new InMemoryUserDetailsManager(user, adminUser, readOnlyUser, serverRestoreUser);
}

public static class InMemoryCondition implements Condition {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/*
* Copyright (C) 2026 European Spallation Source ERIC.
*
*/package org.phoebus.service.saveandrestore.web.config;
*/

package org.phoebus.service.saveandrestore.web.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -57,6 +59,9 @@ public class WebSecurityConfig {
@Value("${role.admin:sar-admin}")
private String roleAdmin;

@Value("${role.server.restore:sar-server-restore}")
private String roleServerRestore;

/**
*
* @return name of regular user role
Expand All @@ -75,6 +80,14 @@ public String roleAdmin() {
return roleAdmin.toUpperCase();
}

/**
*
* @return name of role authorizing server side restore
*/
@Bean
public String roleServerRestore(){
return roleServerRestore;
}

/**
*
Expand Down Expand Up @@ -114,7 +127,7 @@ public WebSecurityCustomizer ignoringCustomizer() {
return web -> {
// The below lists exceptions for authentication.
web.ignoring().requestMatchers(HttpMethod.GET, "/**");
web.ignoring().requestMatchers(HttpMethod.POST, "/**/login*");
web.ignoring().requestMatchers(HttpMethod.POST, "/**/login");
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
/*
* Copyright (C) 2024 European Spallation Source ERIC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* Copyright (C) 2026 European Spallation Source ERIC.
*
*/

package org.phoebus.service.saveandrestore.web.controllers;

import jakarta.servlet.http.HttpServletRequest;
import org.phoebus.applications.saveandrestore.model.LoginCredentials;
import org.phoebus.applications.saveandrestore.model.UserData;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -48,6 +35,9 @@
@Autowired
private AuthenticationManager authenticationManager;

@Autowired

Check warning on line 38 in services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/AuthenticationController.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this field injection and use constructor injection instead.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHple-dI8Q0yZdRnofx&open=AaHple-dI8Q0yZdRnofx&pullRequest=4028
private String roleServerRestore;

/**
* Authenticates user.
*
Expand All @@ -62,12 +52,24 @@
authentication = authenticationManager.authenticate(authentication);
} catch (AuthenticationException e) {
Logger.getLogger(AuthenticationController.class.getName()).log(Level.WARNING, "Unable to authenticate user " + loginCredentials.username(), e);
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).<UserData>build();
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
List<String> roles = authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority).collect(Collectors.toList());
return new ResponseEntity<>(
new UserData(loginCredentials.username(), roles),
HttpStatus.OK);
}


/**
* Checks if a user is authorized to do server side restore. Note that this must be a POST endpoint
* as GET endpoints do not provide information on user authorization.
* @param request A {@link HttpServletRequest} object
* @return <code>true</code> if authorized, i.e. has the associated role.
*/
@PostMapping(value = "server-restore-authorized")
public boolean authorizedForServerRestore(HttpServletRequest request) {
return request.isUserInRole("ROLE_" + roleServerRestore.toUpperCase());
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
/*
* Copyright (C) 2024 European Spallation Source ERIC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* Copyright (C) 2026 European Spallation Source ERIC.
*
*/

Expand Down Expand Up @@ -52,6 +38,9 @@
@Autowired
private String roleUser;

@Autowired

Check warning on line 41 in services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/AuthorizationHelper.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this field injection and use constructor injection instead.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHple-9I8Q0yZdRnofy&open=AaHple-9I8Q0yZdRnofy&pullRequest=4028
private String roleServerRestore;

@Value("${authorization.permitall:true}")
private boolean permitAll;

Expand Down Expand Up @@ -288,4 +277,14 @@
public boolean mayMoveOrCopy(MethodSecurityExpressionOperations methodSecurityExpressionOperations) {
return permitAll || methodSecurityExpressionOperations.hasAuthority(ROLE_PREFIX + roleAdmin);
}

/**
* Checks if user is authorized to perform server side restore.
* @param methodSecurityExpressionOperations {@link MethodSecurityExpressionOperations} Spring managed object
* queried for authorization.
* @return <code>false</code> if user is not authorized to request server side restore, otherwise <code>true</code>.
*/
public boolean hasServerRestoreAuthorization(MethodSecurityExpressionOperations methodSecurityExpressionOperations){
return methodSecurityExpressionOperations.hasAuthority(ROLE_PREFIX + roleServerRestore);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
/**
* Copyright (C) 2018 European Spallation Source ERIC.
* <p>
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* Copyright (C) 2026 European Spallation Source ERIC.
*/
package org.phoebus.service.saveandrestore.web.controllers;

import org.phoebus.service.saveandrestore.NodeNotFoundException;
import org.phoebus.service.saveandrestore.SnapshotNotFoundException;
import org.phoebus.service.saveandrestore.persistence.config.IndexConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -39,7 +26,7 @@
*/
@RestController
@SuppressWarnings("unused")
public abstract class BaseController {
public abstract class BaseController extends IndexConfig {

protected static final String JSON = "application/json";

Expand Down
22 changes: 21 additions & 1 deletion services/save-and-restore/src/main/resources/sar.ldif
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ uidNumber: 23006
gidNumber: 23006
homeDirectory: /dev/null

dn: uid=johndoe,ou=Group,dc=sar,dc=local
dn: uid=johndoe,ou=People,dc=sar,dc=local
uid: johndoe
objectClass: account
objectClass: posixAccount
Expand All @@ -44,6 +44,18 @@ uidNumber: 23007
gidNumber: 23007
homeDirectory: /dev/null

dn: uid=serverRestoreUser,ou=People,dc=sar,dc=local
uid: serverRestoreUser
objectClass: account
objectClass: posixAccount
description: User with server restore authorization
cn: serverRestoreUser
# bcrypt encoded string "1234"
userPassword: $2a$12$.gd4p4gwfNd3S0I1giLWfuEdtBvHCg/B9gi7OmAsyNAF62eWg4xDS
uidNumber: 23008
gidNumber: 23008
homeDirectory: /dev/null

dn: cn=sar-user,ou=Group,dc=sar,dc=local
cn: sar-user
objectClass: posixGroup
Expand All @@ -59,3 +71,11 @@ description: save-n-restore admin
gidNumber: 27003
uidNumber: 27003
memberUid: admin

dn: cn=sar-server-restore,ou=Group,dc=sar,dc=local
cn: sar-server-restore
objectClass: posixGroup
description: restore role
gidNumber: 27004
uidNumber: 27004
memberUid: serverRestoreUser
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ demo.admin=admin
demo.admin.password=adminPass
demo.readOnly=johndoe
demo.readOnlyPassword=1234
demo.server.restore.user=serverRestoreUser
demo.server.restore.password=serverRestoreUserPass
Loading
Loading