Skip to content

Commit

Permalink
sonar fix (#2526)
Browse files Browse the repository at this point in the history
* sonar fix

* more coverage

* more coverage
  • Loading branch information
strehle authored Oct 20, 2023
1 parent 567390d commit 92f90b2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ private static String getEnvOrProperty(String key) {
}
}

@SuppressWarnings("SameParameterValue")
// packageFriendly for Testing
static String[] getLocalConfigDirs( List<String> dirProxies, UnaryOperator<String> unProxyFunction ) {
return dirProxies.stream()
Expand All @@ -83,10 +82,10 @@ static SourcedFile clean( SourcedFile sourcedFile ) {
return str.isEmpty() ? null : new SourcedFile( str, sourcedFile.getSource() );
}

@SuppressWarnings("SameParameterValue")
// packageFriendly for Testing
static InitialConfig create( SourcedFile localConfigFile, NanoTimeSupplier currentTimeSupplier ) {
if (localConfigFile == null) { // Leave everything disabled!
// Leave everything disabled!
if (localConfigFile == null) {
return new InitialConfig( null, null, RateLimitingFactoriesSupplierWithStatus.NO_RATE_LIMITING );
}
String errorMsg = null;
Expand All @@ -99,8 +98,7 @@ static InitialConfig create( SourcedFile localConfigFile, NanoTimeSupplier curre
try {
dto = parseFile( bindYaml, localConfigFile.getBody() );
currentStatus = CurrentStatus.PENDING;
}
catch ( YamlRateLimitingConfigException e ) {
} catch ( YamlRateLimitingConfigException e ) {
error = e;
errorMsg = e.getMessage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@

import org.apache.commons.lang3.StringUtils;

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class SourcedFile {
private final String body;
private final String source;

public SourcedFile(String body, String source) {
this.body = body;
this.source = source;
}

public static SourcedFile locateAndLoadLocalFile( String name, String... dirs ) {
if ( (name == null) || name.isBlank() ) {
return null;
Expand Down Expand Up @@ -48,8 +51,7 @@ static SourcedFile loadFile( InputStream is, String source ) {
for ( String line; (line = reader.readLine()) != null; ) {
sb.append( line ).append( '\n' );
}
}
catch ( IOException e ) {
} catch ( IOException e ) {
throw new IllegalStateException( "Unable to read " + source, e );
}
String str = sb.toString();
Expand All @@ -66,8 +68,7 @@ private static InputStream getFileInputStream( String dir, String name ) {
if ( file.isFile() ) {
return new FileInputStream( file );
}
}
catch ( IOException ignore ) {
} catch ( IOException ignore ) {
// ignore!
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;

class SourcedFileTest {
public static final String EFFECTIVELY_EMPTY_FILE_CONTENTS = "\n \n";
Expand All @@ -31,6 +32,26 @@ void loadFile() {
check( ODD_FILE_CONTENTS, "test-2" );
}

@Test
void loadStream() {
ByteArrayInputStream is = new ByteArrayInputStream(ODD_FILE_CONTENTS.getBytes());
assertNotNull( SourcedFile.loadFile( is, "test-0" ) );
}

@Test
void loadEnv() {
assertNotNull( SourcedFile.locateAndLoadLocalFile("uaa-ratelimit.yml", SourcedFileTest.class.getClassLoader().getResource("uaa-ratelimit.yml").getPath().replace("uaa-ratelimit.yml", "")));
assertNull( SourcedFile.locateAndLoadLocalFile("", SourcedFileTest.class.getClassLoader().getResource("uaa-ratelimit.yml").getPath().replace("uaa-ratelimit.yml", "")));
assertNull( SourcedFile.locateAndLoadLocalFile("random", "/dev"));
assertNull( SourcedFile.locateAndLoadLocalFile("?", "/proc/1/fdinfo"));
}

@Test
void loadStreamException() {
InputStream in = mock(InputStream.class);
assertThrows(IllegalStateException.class, () -> SourcedFile.loadFile( in, "" ) );
}

private void check( String fileContents, String source ) {
SourcedFile sourcedFile = SourcedFile.loadFile( inputStringFrom( fileContents ), source );
assertNotNull( sourcedFile, source );
Expand Down
33 changes: 33 additions & 0 deletions server/src/test/resources/uaa-ratelimit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
ratelimit:
loggingOption: AllCalls
credentialID: 'JWT:Claims+"sub"\s*:\s*"(.*?)"'
limiterMappings:
- name: AuthToken
withCallerRemoteAddressID: 50r/s
pathSelectors:
- "equals:/oauth/token"
- name: AuthAuthorize
withCallerRemoteAddressID: 50r/s
pathSelectors:
- "equals:/oauth/authorize"
- name: LoginPage
withCallerRemoteAddressID: 50r/1s
pathSelectors:
- "equals:/login"
- name: LoginDo
withCallerRemoteAddressID: 50r/s
pathSelectors:
- "equals:/login.do"
- name: InfoLimit
withCallerRemoteAddressID: 20r/s
pathSelectors:
- "equals:/info"
- name: SCIM
withCallerCredentialsID: 500r/s
pathSelectors:
- "startsWith:/Users"
- "startsWith:/Groups"
- name: EverythingElse
global: 200r/s
pathSelectors:
- "other"

0 comments on commit 92f90b2

Please sign in to comment.