Skip to content

Commit

Permalink
JASPIC Provider Framework logging doesn't depend on external bundles
Browse files Browse the repository at this point in the history
- all bundles moved to the module
- fixed some log messages

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Jul 21, 2023
1 parent 35dc4b3 commit e907442
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 135 deletions.
4 changes: 4 additions & 0 deletions appserver/security/jaspic-provider-framework/pom.xml
Expand Up @@ -47,6 +47,10 @@
<groupId>jakarta.authorization</groupId>
<artifactId>jakarta.authorization-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.annotations</groupId>
<artifactId>logging-annotation-processor</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,23 +17,24 @@

package com.sun.jaspic.config.delegate;

import java.util.Map;
import jakarta.security.auth.message.MessageInfo;
import jakarta.security.auth.message.MessagePolicy;

import java.util.Map;

/**
*
* @author ronmonzillo
*/
public interface MessagePolicyDelegate {

public MessagePolicy getRequestPolicy(String authContextID, Map properties);
MessagePolicy getRequestPolicy(String authContextID, Map properties);

public MessagePolicy getResponsePolicy(String authContextID, Map properties);
MessagePolicy getResponsePolicy(String authContextID, Map properties);

public Class[] getMessageTypes();
Class[] getMessageTypes();

public String getAuthContextID(MessageInfo messageInfo);
String getAuthContextID(MessageInfo messageInfo);

public boolean isProtected();
boolean isProtected();
}
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -23,7 +24,7 @@
public class AuthConfigFileFactory extends BaseAuthConfigFactory {

// MUST "hide" regStore in derived class.
static volatile RegStoreFileParser regStore = null;
static volatile RegStoreFileParser regStore;

/**
* to specialize the defaultEntries passed to the RegStoreFileParser
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2022 Contributors to the Eclipse Foundation.
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation.
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -17,6 +17,15 @@

package com.sun.jaspic.config.factory;

import com.sun.jaspic.config.factory.singlemodule.DefaultAuthConfigProvider;
import com.sun.jaspic.config.helper.JASPICLogManager;

import jakarta.security.auth.message.config.AuthConfigFactory;
import jakarta.security.auth.message.config.AuthConfigProvider;
import jakarta.security.auth.message.config.RegistrationListener;
import jakarta.security.auth.message.module.ServerAuthModule;
import jakarta.servlet.ServletContext;

import java.lang.reflect.Constructor;
import java.security.AccessController;
import java.security.PrivilegedAction;
Expand All @@ -33,23 +42,14 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.jaspic.config.factory.singlemodule.DefaultAuthConfigProvider;
import com.sun.jaspic.config.helper.JASPICLogManager;

import jakarta.security.auth.message.config.AuthConfigFactory;
import jakarta.security.auth.message.config.AuthConfigProvider;
import jakarta.security.auth.message.config.RegistrationListener;
import jakarta.security.auth.message.module.ServerAuthModule;
import jakarta.servlet.ServletContext;


/**
* This class implements methods in the abstract class AuthConfigFactory.
* @author Shing Wai Chan
*/
public abstract class BaseAuthConfigFactory extends AuthConfigFactory {

private static final Logger logger = Logger.getLogger(JASPICLogManager.JASPIC_LOGGER, JASPICLogManager.RES_BUNDLE);
private static final Logger LOG = Logger.getLogger(JASPICLogManager.LOGGER, JASPICLogManager.BUNDLE);

private static final String CONTEXT_REGISTRATION_ID = "org.glassfish.security.message.registrationId";

Expand Down Expand Up @@ -555,10 +555,7 @@ private static AuthConfigProvider _constructProvider(String className, Map prope
Constructor<AuthConfigProvider> constr = c.getConstructor(Map.class, AuthConfigFactory.class);
provider = constr.newInstance(new Object[] {properties, factory});
} catch (Throwable t) {
Throwable cause = t.getCause();
logger.log(Level.WARNING,
"jmac.factory_unable_to_load_provider",
new Object[]{ className, t.toString(), (cause == null ? "cannot determine" : cause.toString())});
LOG.log(Level.WARNING, JASPICLogManager.MSG_UNABLE_LOAD_PROVIDER + className , t);
}
}
return provider;
Expand Down Expand Up @@ -681,10 +678,7 @@ protected void _loadFactory() {
}
}
} catch (Exception e) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING,
"jmac.factory_auth_config_loader_failure", e);
}
LOG.log(Level.WARNING, JASPICLogManager.MSG_LOADER_FAILURE, e);
}
}

Expand Down Expand Up @@ -792,7 +786,7 @@ private static Map<String, List<RegistrationListener>> getEffectedListeners(Stri
for (String listenerID : listenerRegistrations) {
if (regIdImplies(regisID, listenerID)) {
if (!effectedListeners.containsKey(listenerID)) {
effectedListeners.put(listenerID, new ArrayList<RegistrationListener>());
effectedListeners.put(listenerID, new ArrayList<>());
}
effectedListeners.get(listenerID).addAll(id2RegisListenersMap.remove(listenerID));
}
Expand Down
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -17,6 +18,9 @@
package com.sun.jaspic.config.factory;

import com.sun.jaspic.config.helper.JASPICLogManager;

import jakarta.security.auth.message.config.AuthConfigFactory.RegistrationContext;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
Expand All @@ -31,8 +35,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import jakarta.security.auth.message.config.AuthConfigFactory.RegistrationContext;


/**
* Used by GFServerConfigProvider to parse the configuration file. If
Expand All @@ -44,8 +46,7 @@
*/
public final class RegStoreFileParser {

private static final Logger logger =
Logger.getLogger(JASPICLogManager.JASPIC_LOGGER, JASPICLogManager.RES_BUNDLE);
private static final Logger LOG = Logger.getLogger(JASPICLogManager.LOGGER, JASPICLogManager.BUNDLE);

private static final String SEP = ":";
private static final String CON_ENTRY = "con-entry";
Expand All @@ -58,7 +59,7 @@ public final class RegStoreFileParser {

private final File confFile;
private List<EntryInfo> entries;
private List<EntryInfo> defaultEntries;
private final List<EntryInfo> defaultEntries;

/*
* Loads the configuration file from the given filename.
Expand All @@ -68,39 +69,23 @@ public final class RegStoreFileParser {
*/
public RegStoreFileParser(String pathParent, String pathChild,List<EntryInfo> defaultEntries) {
confFile = new File(pathParent, pathChild);
this.defaultEntries = defaultEntries == null ? new ArrayList<EntryInfo>() : defaultEntries;
this.defaultEntries = defaultEntries == null ? new ArrayList<>() : defaultEntries;
try {
loadEntries();
} catch (IOException ioe) {
logWarningDefault(ioe);
} catch (IllegalArgumentException iae) {
logWarningDefault(iae);
}
}

private void logWarningUpdated(Exception exception) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING,
"jmac.factory_could_not_persist", exception.toString());
}
}

private void logWarningDefault(Exception exception) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING,
"jmac.factory_could_not_read", exception.toString());
} catch (IOException | IllegalArgumentException e) {
LOG.log(Level.WARNING, JASPICLogManager.MSG_COULD_NOT_READ_AUTH_CFG, e);
}
}

/*
/**
* Returns the in-memory list of entries.
* MUST Hold exclusive lock on calling factory while processing entries
*/
List<EntryInfo> getPersistedEntries() {
return entries;
}

/*
/**
* Adds the provider to the entry list if it is not already
* present, creates the configuration file if necessary, and
* writes the entries to the file.
Expand All @@ -110,14 +95,14 @@ void store(String className, RegistrationContext ctx, Map properties) {
if (checkAndAddToList(className, ctx, properties)) {
try {
writeEntries();
} catch (IOException ioe) {
logWarningUpdated(ioe);
} catch (IOException e) {
LOG.log(Level.WARNING, JASPICLogManager.MSG_CANNOT_PERSIST_PROVIDERS, e);
}
}
}
}

/*
/**
* Removes the provider from the entry list if it is already
* present, creates the configuration file if necessary, and
* writes the entries to the file.
Expand All @@ -127,14 +112,14 @@ void delete(RegistrationContext ctx) {
if (checkAndRemoveFromList(ctx)) {
try {
writeEntries();
} catch (IOException ioe) {
logWarningUpdated(ioe);
} catch (IOException e) {
LOG.log(Level.WARNING, JASPICLogManager.MSG_CANNOT_PERSIST_PROVIDERS, e);
}
}
}
}

/*
/**
* If this entry does not exist, this method stores it in
* the entries list and returns true to indicate that the
* configuration file should be written.
Expand Down Expand Up @@ -165,7 +150,7 @@ private boolean checkAndAddToList(String className,
return true;
}

/*
/**
* If this registration context does not exist, this method
* returns false. Otherwise it removes the entry and returns
* true to indicate that the configuration file should be written.
Expand Down Expand Up @@ -202,7 +187,7 @@ private boolean checkAndRemoveFromList(RegistrationContext target) {
return retValue;
}

/*
/**
* Used to find a matching registration entry in the 'entries'
* list without including registration contexts. If there is not
* a matching entry, return null.
Expand All @@ -216,15 +201,13 @@ private EntryInfo getMatchingRegEntry(EntryInfo target) {
return null;
}

/*
/**
* This method overwrites the existing file with the
* current entries.
*/
private void writeEntries() throws IOException {
if (confFile.exists() && !confFile.canWrite()
&& logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "jmac.factory_cannot_write_file",
confFile.getPath());
if (confFile.exists() && !confFile.canWrite()) {
LOG.log(Level.WARNING, JASPICLogManager.MSG_CANNOT_WRITE_PROVIDERS_TO_FILE, confFile);
}
clearExistingFile();
PrintWriter out = new PrintWriter(confFile);
Expand All @@ -239,7 +222,7 @@ private void writeEntries() throws IOException {
out.close();
}

/*
/**
* Writes constructor entry output of the form:
* <pre>
* con-entry {
Expand All @@ -265,7 +248,7 @@ private void writeConEntry(EntryInfo info, PrintWriter out, int i) {
out.println(INDENT[--i] + "}");
}

/*
/**
* Write registration entry output of the form:
* <pre>
* reg-entry {
Expand Down Expand Up @@ -308,22 +291,21 @@ private void clearExistingFile() throws IOException {
}
}
if (newCreation) {
logger.log(Level.INFO, "jmac.factory_creating_conf_file",
confFile.getPath());
LOG.log(Level.INFO, JASPICLogManager.MSG_CREATING_JMAC_FILE, confFile);
}
if (!confFile.createNewFile()) {
throw new IOException();
}
}

/*
/**
* Called from the constructor. This is the only time
* the file is read, though it is written when new
* entries are stored or deleted.
*/
private void loadEntries() throws IOException {
synchronized (confFile) {
entries = new ArrayList<EntryInfo>();
entries = new ArrayList<>();
if (confFile.exists()) {
try (BufferedReader reader = new BufferedReader(new FileReader(confFile))) {
String line = reader.readLine();
Expand All @@ -338,12 +320,7 @@ private void loadEntries() throws IOException {
}
}
} else {
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, "jmac.factory_file_not_found",
confFile.getParent() + File.pathSeparator
+ confFile.getPath());

}
LOG.log(Level.FINER, JASPICLogManager.MSG_FILE_NOT_EXIST, confFile);
for (EntryInfo e : defaultEntries) {
entries.add(new EntryInfo(e));
}
Expand All @@ -361,7 +338,7 @@ private EntryInfo readConEntry(BufferedReader reader) throws IOException {
return new EntryInfo(className, properties);
}

/*
/**
* Properties must be of the form "key:value." While the key
* String cannot contain a ":" character, the value can. The
* line will be broken into key and value based on the first
Expand All @@ -378,7 +355,7 @@ private Map<String, String> readProperties(BufferedReader reader)
if ("}".equals(line)) {
return null;
}
Map<String, String> properties = new HashMap<String, String>();
Map<String, String> properties = new HashMap<>();
while (!"}".equals(line)) {
properties.put(line.substring(0, line.indexOf(SEP)),
line.substring(line.indexOf(SEP) + 1, line.length()));
Expand All @@ -394,7 +371,7 @@ private EntryInfo readRegEntry(BufferedReader reader) throws IOException {
String className = null;
Map<String, String> properties = null;
List<RegistrationContext> ctxs =
new ArrayList<RegistrationContext>();
new ArrayList<>();
String line = reader.readLine();
if(line != null) {
line = line.trim();
Expand Down

0 comments on commit e907442

Please sign in to comment.