added hangar config options

This commit is contained in:
Jake Potrebic 2020-07-28 12:29:49 -07:00 committed by MiniDigger
parent 76c49a5006
commit 70b3d97b3c
7 changed files with 605 additions and 133 deletions

View File

@ -1,110 +1,535 @@
package me.minidigger.hangar.config;
import me.minidigger.hangar.model.Color;
import me.minidigger.hangar.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import me.minidigger.hangar.model.Color;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
@Configuration
@ConfigurationProperties(prefix = "hangar")
@ComponentScan("me.minidigger.hangar")
public class HangarConfig {
@Value("${hangar.debug:false}")
private boolean debug;
private boolean debug = false;
private int debugLevel = 3;
private boolean staging = true;
private boolean logTimings = false;
@Value("${fakeUser.enabled:false}")
private boolean fakeUserEnabled;
@Value("${fakeUser.id:-1}")
private long fakeUserId;
@Value("${fakeUser.name:paper}")
private String fakeUserName;
@Value("${fakeUser.username:paper}")
private String fakeUserUserName;
@Value("${fakeUser.email:paper@papermc.io}")
private String fakeUserEmail;
@NestedConfigurationProperty
public final FakeUserConfig fakeUser;
@NestedConfigurationProperty
public HangarHomepageConfig homepage;
@NestedConfigurationProperty
public HangarChannelsConfig channels;
@NestedConfigurationProperty
public HangarPagesConfig pages;
@NestedConfigurationProperty
public HangarProjectsConfig projects;
@NestedConfigurationProperty
public HangarUserConfig user;
@NestedConfigurationProperty
public HangarOrgConfig org;
@NestedConfigurationProperty
public HangarApiConfig api;
@Value("${session.expiration:1209600}") // 14 days
private long sessionExpiration;
@Autowired
public HangarConfig(FakeUserConfig fakeUser, HangarHomepageConfig homepage, HangarChannelsConfig channels, HangarPagesConfig pages, HangarProjectsConfig projects, HangarUserConfig user, HangarOrgConfig org, HangarApiConfig api) {
this.fakeUser = fakeUser;
this.homepage = homepage;
this.channels = channels;
this.pages = pages;
this.projects = projects;
this.user = user;
this.org = org;
this.api = api;
}
public boolean isDebug() {
return debug;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
public int getDebugLevel() {
return debugLevel;
}
public void setDebugLevel(int debugLevel) {
this.debugLevel = debugLevel;
}
public boolean isStaging() {
return staging;
}
public void setStaging(boolean staging) {
this.staging = staging;
}
public boolean isLogTimings() {
return logTimings;
}
public void setLogTimings(boolean logTimings) {
this.logTimings = logTimings;
}
@Component
@ConfigurationProperties(prefix = "fake-user")
public static class FakeUserConfig {
private boolean enabled = true;
private long id = -1;
private String name = "paper";
private String username = "paper";
private String email = "paper@papermc.io";
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
@Component
@ConfigurationProperties(prefix = "hangar.channels")
public static class HangarChannelsConfig {
private int maxNameLen = 15;
private String nameRegex = "^[a-zA-Z0-9]+$";
@Value("#{T(me.minidigger.hangar.model.Color).getById(${hangar.channels.color-default})}")
private Color colorDefault = Color.getById(7);
private String nameDefault = "Release";
public int getMaxNameLen() {
return maxNameLen;
}
public void setMaxNameLen(int maxNameLen) {
this.maxNameLen = maxNameLen;
}
public String getNameRegex() {
return nameRegex;
}
public void setNameRegex(String nameRegex) {
this.nameRegex = nameRegex;
}
public Color getColorDefault() {
return colorDefault;
}
public void setColorDefault(Color colorDefault) {
this.colorDefault = colorDefault;
}
public String getNameDefault() {
return nameDefault;
}
public void setNameDefault(String nameDefault) {
this.nameDefault = nameDefault;
}
}
@Component
@ConfigurationProperties(prefix = "hangar.homepage")
public static class HangarHomepageConfig {
private String updateInterval = "10m";
public String getUpdateInterval() {
return updateInterval;
}
public void setUpdateInterval(String updateInterval) {
this.updateInterval = updateInterval;
}
}
@Component
@ConfigurationProperties(prefix = "hangar.pages")
public static class HangarPagesConfig {
@NestedConfigurationProperty
public Home home;
private int minLen = 15;
private int maxLen = 32000;
@NestedConfigurationProperty
public Page page;
@Autowired
public HangarPagesConfig(Home home, Page page) {
this.home = home;
this.page = page;
}
@Component
@ConfigurationProperties(prefix = "hangar.pages.home")
public static class Home {
private String name = "Home";
private String message = "Welcome to your new project!";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
@Component
@ConfigurationProperties(prefix = "hangar.pages.page")
public static class Page {
private int maxLen = 75000;
public int getMaxLen() {
return maxLen;
}
public void setMaxLen(int maxLen) {
this.maxLen = maxLen;
}
}
public int getMinLen() {
return minLen;
}
public void setMinLen(int minLen) {
this.minLen = minLen;
}
public int getMaxLen() {
return maxLen;
}
public void setMaxLen(int maxLen) {
this.maxLen = maxLen;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
}
@Component
@ConfigurationProperties(prefix = "hangar.projects")
public static class HangarProjectsConfig {
private int maxNameLen = 25;
private int maxPages = 50;
private int maxChannels = 5;
private int initLoad = 25;
private int initVersionLoad = 10;
private int maxDescLen = 120;
private boolean fileValidate = true;
private String staleAge = "28d";
private String checkInterval = "1h";
private String draftExpire = "1d";
private int userGridPageSize = 30;
public int getMaxNameLen() {
return maxNameLen;
}
public void setMaxNameLen(int maxNameLen) {
this.maxNameLen = maxNameLen;
}
public int getMaxPages() {
return maxPages;
}
public void setMaxPages(int maxPages) {
this.maxPages = maxPages;
}
public int getMaxChannels() {
return maxChannels;
}
public void setMaxChannels(int maxChannels) {
this.maxChannels = maxChannels;
}
public int getInitLoad() {
return initLoad;
}
public void setInitLoad(int initLoad) {
this.initLoad = initLoad;
}
public int getInitVersionLoad() {
return initVersionLoad;
}
public void setInitVersionLoad(int initVersionLoad) {
this.initVersionLoad = initVersionLoad;
}
public int getMaxDescLen() {
return maxDescLen;
}
public void setMaxDescLen(int maxDescLen) {
this.maxDescLen = maxDescLen;
}
public boolean isFileValidate() {
return fileValidate;
}
public void setFileValidate(boolean fileValidate) {
this.fileValidate = fileValidate;
}
public String getStaleAge() {
return staleAge;
}
public void setStaleAge(String staleAge) {
this.staleAge = staleAge;
}
public String getCheckInterval() {
return checkInterval;
}
public void setCheckInterval(String checkInterval) {
this.checkInterval = checkInterval;
}
public String getDraftExpire() {
return draftExpire;
}
public void setDraftExpire(String draftExpire) {
this.draftExpire = draftExpire;
}
public int getUserGridPageSize() {
return userGridPageSize;
}
public void setUserGridPageSize(int userGridPageSize) {
this.userGridPageSize = userGridPageSize;
}
}
@Component
@ConfigurationProperties(prefix = "hangar.users")
public static class HangarUserConfig {
private int starsPerPage = 5;
private int maxTaglineLen = 100;
private int authorPageSize = 25;
private int projectPageSize = 5;
public int getStarsPerPage() {
return starsPerPage;
}
public void setStarsPerPage(int starsPerPage) {
this.starsPerPage = starsPerPage;
}
public int getMaxTaglineLen() {
return maxTaglineLen;
}
public void setMaxTaglineLen(int maxTaglineLen) {
this.maxTaglineLen = maxTaglineLen;
}
public int getAuthorPageSize() {
return authorPageSize;
}
public void setAuthorPageSize(int authorPageSize) {
this.authorPageSize = authorPageSize;
}
public int getProjectPageSize() {
return projectPageSize;
}
public void setProjectPageSize(int projectPageSize) {
this.projectPageSize = projectPageSize;
}
}
@Component
@ConfigurationProperties(prefix = "hangar.orgs")
public static class HangarOrgConfig {
private boolean enabled = true;
private String dummyEmailDomain = "org.papermc.io";
private int createLimit = 5;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getDummyEmailDomain() {
return dummyEmailDomain;
}
public void setDummyEmailDomain(String dummyEmailDomain) {
this.dummyEmailDomain = dummyEmailDomain;
}
public int getCreateLimit() {
return createLimit;
}
public void setCreateLimit(int createLimit) {
this.createLimit = createLimit;
}
}
@Component
@ConfigurationProperties(prefix = "hangar.queue")
public static class HangarQueueConfig {
private String maxReviewTime = "1d";
public String getMaxReviewTime() {
return maxReviewTime;
}
public void setMaxReviewTime(String maxReviewTime) {
this.maxReviewTime = maxReviewTime;
}
}
@Component
@ConfigurationProperties(prefix = "hangar.api")
public static class HangarApiConfig {
@NestedConfigurationProperty
public Session session;
@Autowired
public HangarApiConfig(Session session) {
this.session = session;
}
@Component
@ConfigurationProperties(prefix = "hangar.api.session")
public static class Session {
private String publicExpiration = "3h";
@DurationUnit(ChronoUnit.DAYS)
private Duration expiration = Duration.ofDays(14);
private String checkInterval = "5m";
public String getPublicExpiration() {
return publicExpiration;
}
public void setPublicExpiration(String publicExpiration) {
this.publicExpiration = publicExpiration;
}
public Duration getExpiration() {
return expiration;
}
public void setExpiration(Duration expiration) {
this.expiration = expiration;
}
public String getCheckInterval() {
return checkInterval;
}
public void setCheckInterval(String checkInterval) {
this.checkInterval = checkInterval;
}
}
}
@Value("${pluginUploadDir:/work/uploads}")
private String pluginUploadDir;
@Value("${defaultChannelName:Release}")
private String defaultChannelName;
@Value("${defaultChannelColor:7}")
private int defaultChannelColor;
@Value("${authorPageSize:25}")
private long authorPageSize;
@Value("${maxTaglineLen:100}")
private long maxTaglineLen;
@Value("${authUrl:https://hangarauth.minidigger.me}")
private String authUrl;
@Value("${maxProjectNameLen:25}")
private long maxProjectNameLen;
public boolean isFakeUserEnabled() {
return fakeUserEnabled;
}
public long getFakeUserId() {
return fakeUserId;
}
public String getFakeUserName() {
return fakeUserName;
}
public String getFakeUserUserName() {
return fakeUserUserName;
}
public String getFakeUserEmail() {
return fakeUserEmail;
}
public long getSessionExpiration() {
return sessionExpiration;
}
public String getPluginUploadDir() {
return pluginUploadDir;
}
public String getDefaultChannelName() {
return defaultChannelName;
}
public Color getDefaultChannelColor() {
return Color.getById(defaultChannelColor);
}
public void checkDebug() {
if (!debug) {
throw new UnsupportedOperationException("this function is supported in debug mode only");
}
}
public long getAuthorPageSize() {
return authorPageSize;
}
public long getMaxTaglineLen() {
return maxTaglineLen;
}
public String getAuthUrl() {
return authUrl;
}
public boolean isValidProjectName(String name) {
String sanitized = StringUtils.compact(name);
return sanitized.length() >= 1 && sanitized.length() <= maxProjectNameLen;
return sanitized.length() >= 1 && sanitized.length() <= projects.maxNameLen;
}
}

View File

@ -9,7 +9,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.ModelAndView;
@ -50,13 +49,13 @@ public class UsersController extends HangarController {
mav.addObject("authors", userService.getAuthors(page, sort));
mav.addObject("ordering", sort);
mav.addObject("page", page);
mav.addObject("pageSize", hangarConfig.getAuthorPageSize());
mav.addObject("pageSize", hangarConfig.user.getAuthorPageSize());
return fillModel(mav);
}
@RequestMapping("/login")
public ModelAndView login(@RequestParam(defaultValue = "") String sso, @RequestParam(defaultValue = "") String sig, @RequestParam String returnUrl) {
if (hangarConfig.isFakeUserEnabled()) {
if (hangarConfig.fakeUser.isEnabled()) {
hangarConfig.checkDebug();
authenticationService.loginAsFakeUser();
@ -111,7 +110,7 @@ public class UsersController extends HangarController {
mav.addObject("staff", userService.getStaff(page, sort));
mav.addObject("ordering", sort);
mav.addObject("page", page);
mav.addObject("pageSize", hangarConfig.getAuthorPageSize());
mav.addObject("pageSize", hangarConfig.user.getAuthorPageSize());
return fillModel(mav);
}
@ -147,7 +146,7 @@ public class UsersController extends HangarController {
@Secured("ROLE_USER")
@PostMapping(value = "/{user}/settings/tagline")
public ModelAndView saveTagline(@PathVariable String user, @RequestParam("tagline") String tagline) {
if (tagline.length() > hangarConfig.getMaxTaglineLen()) {
if (tagline.length() > hangarConfig.user.getMaxTaglineLen()) {
ModelAndView mav = showProjects(user);
AlertUtil.showAlert(mav, AlertUtil.ERROR, "error.tagline.tooLong"); // TODO pass length param to key
return new ModelAndView("redirect:" + routeHelper.getRouteUrl("users.showProjects", user));

View File

@ -1,16 +1,5 @@
package me.minidigger.hangar.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.time.OffsetDateTime;
import java.util.UUID;
import me.minidigger.hangar.config.HangarConfig;
import me.minidigger.hangar.db.dao.HangarDao;
import me.minidigger.hangar.db.dao.UserDao;
@ -21,6 +10,16 @@ import me.minidigger.hangar.model.generated.ApiSessionResponse;
import me.minidigger.hangar.model.generated.SessionProperties;
import me.minidigger.hangar.model.generated.SessionType;
import me.minidigger.hangar.security.HangarAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.time.OffsetDateTime;
import java.util.UUID;
@Service
public class AuthenticationService {
@ -39,12 +38,11 @@ public class AuthenticationService {
}
public ApiSessionResponse authenticateDev() {
if (hangarConfig.isFakeUserEnabled()) {
if (hangarConfig.fakeUser.isEnabled()) {
hangarConfig.checkDebug();
OffsetDateTime sessionExpiration = OffsetDateTime.now().plusSeconds(hangarConfig.getSessionExpiration());
OffsetDateTime sessionExpiration = OffsetDateTime.now().plus(hangarConfig.api.session.getExpiration());
String uuidtoken = UUID.randomUUID().toString();
ApiSession session = new ApiSession(uuidtoken, null, hangarConfig.getFakeUserId(), sessionExpiration);
ApiSession session = new ApiSession(uuidtoken, null, hangarConfig.fakeUser.getId(), sessionExpiration);
saveSession(session);
@ -67,7 +65,7 @@ public class AuthenticationService {
public ApiSessionResponse authenticateKeyPublic(SessionProperties properties, long userId) {
// TODO, get properties session expiration and stuff
OffsetDateTime sessionExpiration = OffsetDateTime.now().plusSeconds(hangarConfig.getSessionExpiration());
OffsetDateTime sessionExpiration = OffsetDateTime.now().plus(hangarConfig.api.session.getExpiration());
String uuidtoken = UUID.randomUUID().toString();
ApiSession session = new ApiSession(uuidtoken, null, userId, sessionExpiration);
@ -77,7 +75,7 @@ public class AuthenticationService {
}
public ApiSessionResponse authenticateUser(long userId) {
OffsetDateTime sessionExpiration = OffsetDateTime.now().plusSeconds(hangarConfig.getSessionExpiration());
OffsetDateTime sessionExpiration = OffsetDateTime.now().plus(hangarConfig.api.session.getExpiration());
String uuidtoken = UUID.randomUUID().toString();
ApiSession session = new ApiSession(uuidtoken, null, userId, sessionExpiration);
@ -91,14 +89,15 @@ public class AuthenticationService {
}
public void loginAsFakeUser() {
String username = hangarConfig.getFakeUserName();
String username = hangarConfig.fakeUser.getUsername();
UsersTable userEntry = userDao.get().getByName(username);
System.out.println(hangarConfig.channels.getColorDefault());
if (userEntry == null) {
userEntry = new UsersTable();
userEntry.setEmail(hangarConfig.getFakeUserEmail());
userEntry.setFullName(hangarConfig.getFakeUserName());
userEntry.setName(hangarConfig.getFakeUserUserName());
userEntry.setId(hangarConfig.getFakeUserId());
userEntry.setEmail(hangarConfig.fakeUser.getEmail());
userEntry.setFullName(hangarConfig.fakeUser.getName());
userEntry.setName(hangarConfig.fakeUser.getUsername());
userEntry.setId(hangarConfig.fakeUser.getId());
userEntry.setReadPrompts(new int[0]);
userEntry = userDao.get().insert(userEntry);

View File

@ -81,7 +81,7 @@ public class UserService {
reverse = false;
}
long pageSize = config.getAuthorPageSize();
long pageSize = config.user.getAuthorPageSize();
long offset = (page - 1) * pageSize;
return userDao.get().getAuthors(offset, pageSize, userOrder(reverse, sort));
@ -98,7 +98,7 @@ public class UserService {
reverse = false;
}
long pageSize = config.getAuthorPageSize();
long pageSize = config.user.getAuthorPageSize();
long offset = (page - 1) * pageSize;
return userDao.get().getStaff(offset, pageSize, userOrder(reverse, sort));

View File

@ -48,7 +48,7 @@ public class ProjectFactory {
String slug = StringUtils.slugify(name);
ProjectsTable projectsTable = new ProjectsTable(pluginId, name, slug, ownerUser.getName(), ownerUser.getId(), category, description, Visibility.NEW.getValue());
ProjectChannelsTable channelsTable = new ProjectChannelsTable(hangarConfig.getDefaultChannelName(), hangarConfig.getDefaultChannelColor().getValue(), -1);
ProjectChannelsTable channelsTable = new ProjectChannelsTable(hangarConfig.channels.getNameDefault(), hangarConfig.channels.getColorDefault().getValue(), -1);
InvalidProjectReason invalidProjectReason;
if (!hangarConfig.isValidProjectName(name)) {

View File

@ -1,32 +0,0 @@
###########
# General #
###########
server.port=8080
spring.freemarker.request-context-attribute=rc
############
# DataBase #
############
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/hangar
spring.datasource.username=hangar
spring.datasource.password=hangar
#############
# Fake User #
#############
fakeUser.enabled=true
fakeUser.id=-1
fakeUser.name=paper
fakeUser.username=paper
fakeUser.email=paper@papermc.io
hangar.debug=true
################
# Spring Debug #
################
#debug=true
#logging.level.org.springframework.security=DEBUG

View File

@ -0,0 +1,81 @@
###########
# General #
###########
server:
port: 8080
spring:
freemarker:
request-context-attribute: rc
############
# DataBase #
############
datasource:
platform: postgres
url: jdbc:postgresql://localhost:5432/hangar
username: hangar
password: hangar
#############
# Fake User #
#############
fake-user:
enabled: true
id: -2
name: paper
username: paper
email: paper@papermc.io
hangar:
debug: true
debug-level: 3
staging: true
log-timings: false
homepage:
update-interval: 10m
channels:
max-name-len: 15
name-regex: "^[a-zA-Z0-9]+$"
color-default: 7
name-default: "Release"
pages:
home:
name: "Hello"
message: "Welcome to your new project!"
min-len: 15
max-len: 32000
page:
max-len: 75000
projects:
max-name-len: 25
max-pages: 50
max-channels: 5
init-load: 25
init-version-load: 10
max-desc-len: 120
file-validate: true
stale-age: 28d
check-interval: 1h
draft-expire: 1d
user-grid-page-size: 30
users:
stars-per-page: 5
max-tagline-len: 100
author-page-size: 25
project-page-size: 5
################
# Spring Debug #
################
#debug=true
#logging.level.org.springframework.security=DEBUG