Skip to content

Commit

Permalink
Merge pull request #16 from agentgonzo/JENKINS-53675
Browse files Browse the repository at this point in the history
[JENKINS-53675] Add the ability to disable checks for certain classes only
  • Loading branch information
kohsuke authored Oct 5, 2018
2 parents e1e0d64 + 00c1a05 commit 0721a0c
Show file tree
Hide file tree
Showing 29 changed files with 657 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* <p>
* Single execution of the enforcement check would create at most one instance
* of a given {@link AccessRestriction} type, so instance fields can be used to store
* heavy-weight objects or other indicies that you might need for implementing
* heavy-weight objects or other indices that you might need for implementing
* access control checks.
*
* @author Kohsuke Kawaguchi
Expand Down
5 changes: 5 additions & 0 deletions access-modifier-checker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<artifactId>access-modifier-annotation</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>access-modifier-suppressions</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-debug-all</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>test</groupId>
<artifactId>disable-restrictions-wrong-annotation</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>api</artifactId>
<dependencies>
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>access-modifier-annotation</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package api;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

public class ApiWithRestrictedMethodAndField {

@Restricted(NoExternalUse.class)
public String field;

@Restricted(NoExternalUse.class)
public static void notReallyPublic() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package api;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

@Restricted(NoExternalUse.class)
public class RestrictedApi {

public String field;

public void doNotUse() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package api;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

@Restricted(NoExternalUse.class)
public interface RestrictedInterface {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>test</groupId>
<artifactId>disable-restrictions-wrong-annotation</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>caller</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.kohsuke</groupId>
<artifactId>access-modifier-checker</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package caller;

import api.ApiWithRestrictedMethodAndField;

public class Caller {

@WrongAnnotation(ApiWithRestrictedMethodAndField.class)
public Caller() {
ApiWithRestrictedMethodAndField.notReallyPublic(); // illegal
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package caller;

import api.ApiWithRestrictedMethodAndField;

@WrongAnnotation(ApiWithRestrictedMethodAndField.class)
public class CallerDisabledAtClassLevel {
public CallerDisabledAtClassLevel() {
ApiWithRestrictedMethodAndField.notReallyPublic(); // illegal
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* The MIT License
*
* Copyright (c) 2018, Steve Arch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package caller;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Retention(RUNTIME)
@Documented
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE})
public @interface WrongAnnotation {
Class<?>[] value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals=clean package
invoker.buildResult = failure
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>disable-restrictions-wrong-annotation</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<modules>
<module>api</module>
<module>caller</module>
</modules>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
assert new File(basedir, 'build.log').text.contains('[ERROR] caller/Caller:9 api/ApiWithRestrictedMethodAndField.notReallyPublic()V must not be used')
assert new File(basedir, 'build.log').text.contains('[ERROR] caller/CallerDisabledAtClassLevel:8 api/ApiWithRestrictedMethodAndField.notReallyPublic()V must not be used')
17 changes: 17 additions & 0 deletions access-modifier-checker/src/it/disable-restrictions/api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>test</groupId>
<artifactId>disable-restrictions</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>api</artifactId>
<dependencies>
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>access-modifier-annotation</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package api;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

public class ApiWithRestrictedMethodAndField {

@Restricted(NoExternalUse.class)
public String field;

@Restricted(NoExternalUse.class)
public static void notReallyPublic() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package api;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

@Restricted(NoExternalUse.class)
public class RestrictedApi {

public String field;

public void doNotUse() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package api;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

@Restricted(NoExternalUse.class)
public interface RestrictedInterface {
}
38 changes: 38 additions & 0 deletions access-modifier-checker/src/it/disable-restrictions/caller/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>test</groupId>
<artifactId>disable-restrictions</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>caller</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>access-modifier-suppressions</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.kohsuke</groupId>
<artifactId>access-modifier-checker</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package caller;

import api.ApiWithRestrictedMethodAndField;
import api.RestrictedApi;
import org.kohsuke.accmod.restrictions.suppressions.SuppressRestrictedWarnings;

public class Caller extends ApiWithRestrictedMethodAndField { // This is fine, ApiWithRestrictedMethodAndField itself is not restricted

private RestrictedApi restrictedApi;

@SuppressRestrictedWarnings(ApiWithRestrictedMethodAndField.class)
public Caller() {
ApiWithRestrictedMethodAndField.notReallyPublic(); // illegal but check disabled at the method level
}

@SuppressRestrictedWarnings({RestrictedApi.class, ApiWithRestrictedMethodAndField.class})
private void invalidFieldUse() {
restrictedApi.field = null;
super.field = null;
}

@SuppressRestrictedWarnings(ApiWithRestrictedMethodAndField.class)
public void callerMethod() {
ApiWithRestrictedMethodAndField.notReallyPublic(); // illegal but check disabled at the method level
}

@SuppressRestrictedWarnings(RestrictedApi.class)
public void methodWithRestrictedParameter(RestrictedApi api) {
api.doNotUse(); // illegal but check disabled at the method level
}

@SuppressRestrictedWarnings(RestrictedApi.class)
public RestrictedApi getRestrictedApi() {
return new RestrictedApi(); // illegal but check disabled at the method level
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package caller;

import api.ApiWithRestrictedMethodAndField;
import api.RestrictedApi;
import org.kohsuke.accmod.restrictions.suppressions.SuppressRestrictedWarnings;

@SuppressRestrictedWarnings( {ApiWithRestrictedMethodAndField.class, RestrictedApi.class})
public class CallerDisabledAtClassLevel extends RestrictedApi {

private RestrictedApi restrictedApi;

public CallerDisabledAtClassLevel() {
ApiWithRestrictedMethodAndField.notReallyPublic(); // illegal but check disabled at the class level
}

private void invalidFieldUse() {
restrictedApi.field = null;
super.field = null;
}

public void callerMethod() {
ApiWithRestrictedMethodAndField.notReallyPublic(); // illegal but check disabled at the class level
}

public void methodWithRestrictedParameter(RestrictedApi api) {
api.doNotUse(); // illegal but check disabled at the class level
}

public RestrictedApi getRestrictedApi() {
return new RestrictedApi(); // illegal but check disabled at the class level
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package caller;

import api.RestrictedApi;
import org.kohsuke.accmod.restrictions.suppressions.SuppressRestrictedWarnings;

@SuppressRestrictedWarnings(RestrictedApi.class)
public class RestrictedApiSubclass extends RestrictedApi {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package caller;

import api.RestrictedInterface;
import org.kohsuke.accmod.restrictions.suppressions.SuppressRestrictedWarnings;

@SuppressRestrictedWarnings(RestrictedInterface.class)
public class RestrictedInterfaceImplementation implements RestrictedInterface {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals=clean package
invoker.buildResult = success
Loading

0 comments on commit 0721a0c

Please sign in to comment.