-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #2 by exposing simple REST endpoint with interval, authorized g…
…roupm and day limits for user and system keys
- Loading branch information
Showing
25 changed files
with
508 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/java/com/lmig/forge/stash/ssh/config/PluginSettingsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.lmig.forge.stash.ssh.config; | ||
|
||
import javax.transaction.Transactional; | ||
|
||
import org.apache.commons.lang.Validate; | ||
|
||
import com.atlassian.sal.api.pluginsettings.PluginSettings; | ||
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory; | ||
import com.lmig.forge.stash.ssh.rest.AdminConfigResourceModel; | ||
|
||
public class PluginSettingsService { | ||
public static final String SETTINGS_KEY_AUTHORIZED_GROUP = AdminConfigResourceModel.class.getName() + ".authorizedGroup"; | ||
public static final String SETTINGS_KEY_DAYS_KEEP_USERS = AdminConfigResourceModel.class.getName() + ".daysToKeepUserKeys"; | ||
public static final String SETTINGS_KEY_DAYS_KEEP_BAMBOO = AdminConfigResourceModel.class.getName() + ".daysToKeepBambooKeys"; | ||
public static final String SETTINGS_KEY_MILLIS_INTERVAL = AdminConfigResourceModel.class.getName() + ".millisBetweenRuns"; | ||
|
||
private final PluginSettingsFactory pluginSettingsFactory; | ||
private AdminConfigResourceModel cachedModel; | ||
|
||
public PluginSettingsService(PluginSettingsFactory pluginSettingsFactory) { | ||
this.pluginSettingsFactory = pluginSettingsFactory; | ||
} | ||
|
||
@Transactional | ||
public AdminConfigResourceModel getAdminConfigResourcesModel(){ | ||
if(null != cachedModel){ | ||
return cachedModel; | ||
} | ||
PluginSettings settings = pluginSettingsFactory.createGlobalSettings(); | ||
AdminConfigResourceModel config = new AdminConfigResourceModel(); | ||
|
||
String authorizedGroup = (String) settings.get(SETTINGS_KEY_AUTHORIZED_GROUP); | ||
if (authorizedGroup != null) | ||
{ | ||
config.setAuthorizedGroup(authorizedGroup); | ||
} | ||
|
||
String daysToKeepUserKeys = (String) settings.get(SETTINGS_KEY_DAYS_KEEP_USERS); | ||
if (daysToKeepUserKeys != null) | ||
{ | ||
config.setDaysToKeepUserKeys(Integer.parseInt(daysToKeepUserKeys)); | ||
} | ||
String daysToKeepBambooKeys = (String) settings.get(SETTINGS_KEY_DAYS_KEEP_BAMBOO); | ||
if (daysToKeepBambooKeys != null) | ||
{ | ||
config.setDaysToKeepBambooKeys(Integer.parseInt(daysToKeepBambooKeys)); | ||
} | ||
String millisBetweenRuns = (String) settings.get(SETTINGS_KEY_MILLIS_INTERVAL); | ||
if (millisBetweenRuns != null) | ||
{ | ||
config.setMillisBetweenRuns(Long.parseLong(millisBetweenRuns)); | ||
} | ||
return config; | ||
} | ||
|
||
@Transactional | ||
public AdminConfigResourceModel updateAdminConfigResourcesModel(final AdminConfigResourceModel updatedConfig) { | ||
|
||
Validate.isTrue(updatedConfig.getDaysToKeepBambooKeys() >= 1,"must keep keys at least 1 day"); | ||
Validate.isTrue(updatedConfig.getDaysToKeepUserKeys() >= 1,"must keep keys at least 1 day"); | ||
Validate.isTrue(updatedConfig.getMillisBetweenRuns() >= 60000,"Must space at least 1 minute apart"); | ||
PluginSettings settings = pluginSettingsFactory.createGlobalSettings(); | ||
|
||
settings.put(SETTINGS_KEY_AUTHORIZED_GROUP, updatedConfig.getAuthorizedGroup()); | ||
settings.put(SETTINGS_KEY_DAYS_KEEP_USERS, String.valueOf(updatedConfig.getDaysToKeepUserKeys())); | ||
settings.put(SETTINGS_KEY_DAYS_KEEP_BAMBOO, String.valueOf(updatedConfig.getDaysToKeepBambooKeys())); | ||
settings.put(SETTINGS_KEY_MILLIS_INTERVAL, String.valueOf(updatedConfig.getMillisBetweenRuns())); | ||
|
||
//refresh cache, including logic on default values in case some were not provided. | ||
cachedModel = updatedConfig; | ||
return cachedModel; | ||
|
||
} | ||
|
||
public String getAuthorizedGroup() { | ||
return getAdminConfigResourcesModel().getAuthorizedGroup(); | ||
} | ||
|
||
public int getDaysAllowedForUserKeys() { | ||
return getAdminConfigResourcesModel().getDaysToKeepUserKeys(); | ||
} | ||
public int getDaysAllowedForBambooKeys() { | ||
return getAdminConfigResourcesModel().getDaysToKeepBambooKeys(); | ||
} | ||
public long getMillisBetweenRuns() { | ||
return getAdminConfigResourcesModel().getMillisBetweenRuns(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/com/lmig/forge/stash/ssh/rest/AdminConfigResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.lmig.forge.stash.ssh.rest; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.PUT; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
import com.atlassian.sal.api.user.UserManager; | ||
import com.atlassian.stash.user.StashAuthenticationContext; | ||
import com.atlassian.stash.user.StashUser; | ||
import com.lmig.forge.stash.ssh.config.PluginSettingsService; | ||
|
||
/** | ||
* A resource of message. | ||
*/ | ||
@Path("/config") | ||
public class AdminConfigResource { | ||
|
||
private final UserManager userManager; | ||
private final StashAuthenticationContext stashAuthenticationContext; | ||
private final PluginSettingsService pluginSettingsService; | ||
|
||
|
||
public AdminConfigResource(UserManager userManager, StashAuthenticationContext stashAuthenticationContext, | ||
PluginSettingsService pluginSettingsService) { | ||
super(); | ||
this.userManager = userManager; | ||
this.stashAuthenticationContext = stashAuthenticationContext; | ||
this.pluginSettingsService = pluginSettingsService; | ||
} | ||
|
||
@GET | ||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) | ||
public Response getConfig() | ||
{ | ||
StashUser user = stashAuthenticationContext.getCurrentUser(); | ||
if (user == null || !userManager.isSystemAdmin(user.getName())){ | ||
return Response.status(Response.Status.FORBIDDEN).build(); | ||
} | ||
return Response.ok( pluginSettingsService.getAdminConfigResourcesModel()).build(); | ||
} | ||
|
||
@PUT | ||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) | ||
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) | ||
public Response updateConfig(AdminConfigResourceModel updatedModel) | ||
{ | ||
StashUser user = stashAuthenticationContext.getCurrentUser(); | ||
if (user == null || !userManager.isSystemAdmin(user.getName())){ | ||
return Response.status(Response.Status.FORBIDDEN).build(); | ||
} | ||
|
||
return Response.ok( pluginSettingsService.updateAdminConfigResourcesModel(updatedModel)).build(); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/com/lmig/forge/stash/ssh/rest/AdminConfigResourceModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.lmig.forge.stash.ssh.rest; | ||
|
||
import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
import javax.xml.bind.annotation.XmlElement; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
@XmlRootElement(name = "config") | ||
@XmlAccessorType(XmlAccessType.FIELD) | ||
public class AdminConfigResourceModel { | ||
|
||
public static final int DEFAULT_DAYS_USER = 90; | ||
public static final int DEFAULT_DAYS_BAMBOO = 365; | ||
public static final long DEFAULT_MILLIS_BETWEEN_RUNS = 1000 * 60 * 60 * 24; //one day | ||
@XmlElement(name = "message") | ||
private String message; | ||
@XmlElement(name = "authorizedGroup") | ||
private String authorizedGroup; | ||
@XmlElement(name = "daysToKeepUserKeys") | ||
private int daysToKeepUserKeys = DEFAULT_DAYS_USER; | ||
@XmlElement(name = "daysToKeepBambooKeys") | ||
private int daysToKeepBambooKeys = DEFAULT_DAYS_BAMBOO; | ||
@XmlElement(name = "millisBetweenRuns") | ||
private long millisBetweenRuns = DEFAULT_MILLIS_BETWEEN_RUNS; | ||
|
||
public AdminConfigResourceModel() { | ||
} | ||
|
||
public AdminConfigResourceModel(String message, String authorizedGroup, int daysToKeepUserKeys, | ||
int daysToKeepBambooKeys, long millisBetweenRuns) { | ||
this.message = message; | ||
this.authorizedGroup = authorizedGroup; | ||
this.daysToKeepUserKeys = daysToKeepUserKeys; | ||
this.daysToKeepBambooKeys = daysToKeepBambooKeys; | ||
this.millisBetweenRuns = millisBetweenRuns; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getAuthorizedGroup() { | ||
return authorizedGroup; | ||
} | ||
|
||
public void setAuthorizedGroup(String authorizedGroup) { | ||
this.authorizedGroup = authorizedGroup; | ||
} | ||
|
||
public int getDaysToKeepUserKeys() { | ||
return daysToKeepUserKeys; | ||
} | ||
|
||
public void setDaysToKeepUserKeys(int daysToKeepUserKeys) { | ||
this.daysToKeepUserKeys = daysToKeepUserKeys; | ||
} | ||
|
||
public int getDaysToKeepBambooKeys() { | ||
return daysToKeepBambooKeys; | ||
} | ||
|
||
public void setDaysToKeepBambooKeys(int daysToKeepBambooKeys) { | ||
this.daysToKeepBambooKeys = daysToKeepBambooKeys; | ||
} | ||
|
||
public long getMillisBetweenRuns() { | ||
return millisBetweenRuns; | ||
} | ||
|
||
public void setMillisBetweenRuns(long millisBetweenRuns) { | ||
this.millisBetweenRuns = millisBetweenRuns; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.