Search in sources :

Example 16 with Key

use of com.predic8.membrane.core.interceptor.apimanagement.Key in project service-proxy by membrane.

the class ApiManagementConfigurationTest method testParseYaml.

@Test
public void testParseYaml() throws Exception {
    String source = new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir") + "\\src\\test\\resources\\apimanagement\\api.yaml")), Charset.defaultCharset());
    ApiManagementConfiguration conf = new ApiManagementConfiguration();
    conf.setLocation(source);
    Map<String, Policy> policies = conf.getPolicies();
    for (Policy p : policies.values()) {
        System.out.println(p);
    }
    Map<String, Key> keys = conf.getKeys();
    for (Key k : keys.values()) {
        System.out.println(k);
    }
}
Also used : Policy(com.predic8.membrane.core.interceptor.apimanagement.policy.Policy) Test(org.junit.Test)

Example 17 with Key

use of com.predic8.membrane.core.interceptor.apimanagement.Key in project service-proxy by membrane.

the class ApiManagementConfiguration method parsePoliciesForKeys.

private Map<String, Key> parsePoliciesForKeys(Map<String, Object> yaml) {
    Map<String, Key> result = new HashMap<String, Key>();
    // assumption: the yaml is valid
    List<Object> keys = (List<Object>) yaml.get("keys");
    if (keys == null) {
        log.info("No API keys in policy file");
        return result;
    }
    for (Object keyObject : keys) {
        LinkedHashMap<String, Object> key = (LinkedHashMap<String, Object>) keyObject;
        Key keyRes = new Key();
        String keyName = (String) key.get("key");
        keyRes.setName(keyName);
        parseExpiration(key, keyRes);
        List<Object> policiesForKey = (List<Object>) key.get("policies");
        HashSet<Policy> policies = new HashSet<Policy>();
        for (Object polObj : policiesForKey) {
            String policyName = (String) polObj;
            Policy p = this.policies.get(policyName);
            policies.add(p);
        }
        keyRes.setPolicies(policies);
        result.put(keyName, keyRes);
    }
    return result;
}
Also used : Policy(com.predic8.membrane.core.interceptor.apimanagement.policy.Policy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 18 with Key

use of com.predic8.membrane.core.interceptor.apimanagement.Key in project service-proxy by membrane.

the class AMRateLimiter method addRequestEntry.

private void addRequestEntry(String apiKey) {
    synchronized (keyInformation) {
        if (!keyInformation.containsKey(apiKey)) {
            ApiKeyRequestCounter value = new ApiKeyRequestCounter();
            Key key = amc.getKeys().get(apiKey);
            for (Policy p : key.getPolicies()) {
                value.getPolicyCounters().put(p.getName(), new AtomicInteger());
            }
            keyInformation.put(apiKey, value);
        }
    }
    ApiKeyRequestCounter keyInfo = keyInformation.get(apiKey);
    for (AtomicInteger counter : keyInfo.getPolicyCounters().values()) {
        counter.incrementAndGet();
    }
}
Also used : Policy(com.predic8.membrane.core.interceptor.apimanagement.policy.Policy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Key(com.predic8.membrane.core.interceptor.apimanagement.Key)

Example 19 with Key

use of com.predic8.membrane.core.interceptor.apimanagement.Key in project service-proxy by membrane.

the class OtpProvider method computePin.

/**
 * Computes the one-time PIN given the secret key.
 *
 * @param secret
 *            the secret key
 * @param otp_state
 *            current token state (counter or time-interval)
 * @param challenge
 *            optional challenge bytes to include when computing passcode.
 * @return the PIN
 */
private String computePin(String secret, long otp_state) {
    if (secret == null || secret.length() == 0) {
        throw new RuntimeException("Null or empty secret");
    }
    try {
        Signer signer = getSigningOracle(secret);
        PasscodeGenerator pcg = new PasscodeGenerator(signer, PIN_LENGTH);
        return pcg.generateResponseCode(otp_state);
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Crypto failure", e);
    }
}
Also used : Signer(com.predic8.membrane.core.interceptor.authentication.session.totp.PasscodeGenerator.Signer) GeneralSecurityException(java.security.GeneralSecurityException)

Example 20 with Key

use of com.predic8.membrane.core.interceptor.apimanagement.Key in project service-proxy by membrane.

the class HttpServletHandler method createHeader.

private Header createHeader() {
    Header header = new Header();
    Enumeration<?> e = request.getHeaderNames();
    while (e.hasMoreElements()) {
        String key = (String) e.nextElement();
        Enumeration<?> e2 = request.getHeaders(key);
        while (e2.hasMoreElements()) {
            String value = (String) e2.nextElement();
            header.add(key, value);
        }
    }
    return header;
}
Also used : Header(com.predic8.membrane.core.http.Header)

Aggregations

Policy (com.predic8.membrane.core.interceptor.apimanagement.policy.Policy)5 RuleKey (com.predic8.membrane.core.rules.RuleKey)3 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)3 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)3 IOException (java.io.IOException)3 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)2 Request (com.predic8.membrane.core.http.Request)2 Key (com.predic8.membrane.core.interceptor.apimanagement.Key)2 ResourceRetrievalException (com.predic8.membrane.core.resolver.ResourceRetrievalException)2 StatisticCollector (com.predic8.membrane.core.rules.StatisticCollector)2 MalformedURLException (java.net.MalformedURLException)2 MCAttribute (com.predic8.membrane.annot.MCAttribute)1 MCChildElement (com.predic8.membrane.annot.MCChildElement)1 MCElement (com.predic8.membrane.annot.MCElement)1 MCOtherAttributes (com.predic8.membrane.annot.MCOtherAttributes)1 MCTextContent (com.predic8.membrane.annot.MCTextContent)1 MainInfo (com.predic8.membrane.annot.model.MainInfo)1 HttpRouter (com.predic8.membrane.core.HttpRouter)1 SSLParser (com.predic8.membrane.core.config.security.SSLParser)1 Exchange (com.predic8.membrane.core.exchange.Exchange)1