2222import java .io .InputStreamReader ;
2323import java .nio .charset .StandardCharsets ;
2424import java .util .ArrayList ;
25- import java .util .Collections ;
2625import java .util .HashSet ;
2726import java .util .List ;
27+ import java .util .Map ;
2828import java .util .Optional ;
2929import java .util .Set ;
30+ import java .util .concurrent .ConcurrentHashMap ;
3031import java .util .function .Predicate ;
3132import javax .annotation .Nullable ;
3233import org .slf4j .Logger ;
@@ -44,26 +45,31 @@ public class ResourceAccessPolicyCheck extends AbstractIamPolicyStatementCheck {
4445 private static final Logger LOG = LoggerFactory .getLogger (ResourceAccessPolicyCheck .class );
4546 private static final String MESSAGE = "Make sure granting access to all resources is safe here." ;
4647 private static final String SECONDARY_MESSAGE = "Related effect" ;
48+ private static final Map <String , Set <String >> CACHED_RESOURCES = new ConcurrentHashMap <>();
4749 // visible for testing
4850 String resourceNameSensitiveAwsActions = "ResourceAccessPolicyCheck.txt" ;
4951 private Set <String > sensitiveAwsActions = null ;
5052
5153 void init () {
52- try {
53- sensitiveAwsActions = new HashSet <>(loadResource (resourceNameSensitiveAwsActions ));
54- } catch (IOException e ) {
55- sensitiveAwsActions = Collections .emptySet ();
56- LOG .error ("Couldn't load resource '" + resourceNameSensitiveAwsActions + "', rule [S6304] ResourceAccessPolicyCheck will be disabled." , e );
57- }
58- }
54+ sensitiveAwsActions = CACHED_RESOURCES .computeIfAbsent (resourceNameSensitiveAwsActions , ResourceAccessPolicyCheck ::loadResourceWrapper );
5955
56+ }
6057 @ Override
6158 public void initialize (SubscriptionCheck .Context context ) {
6259 super .initialize (context );
6360 init ();
6461 }
6562
66- private static List <String > loadResource (String resourceName ) throws IOException {
63+ private static Set <String > loadResourceWrapper (String resourceName ) {
64+ try {
65+ return loadResource (resourceName );
66+ } catch (IOException e ) {
67+ LOG .error ("Couldn't load resource '{}', rule [S6304] ResourceAccessPolicyCheck will be disabled." , resourceName , e );
68+ return Set .of ();
69+ }
70+ }
71+
72+ private static Set <String > loadResource (String resourceName ) throws IOException {
6773 try (InputStream is = ResourceAccessPolicyCheck .class .getResourceAsStream (resourceName )) {
6874 if (is == null ) {
6975 throw new IOException ("Cannot find resource file '" + resourceName + "'" );
@@ -75,7 +81,7 @@ private static List<String> loadResource(String resourceName) throws IOException
7581 while ((line = br .readLine ()) != null ) {
7682 result .add (line );
7783 }
78- return result ;
84+ return new HashSet <>( result ) ;
7985 }
8086 }
8187 }
0 commit comments