aboutsummaryrefslogtreecommitdiffstats
path: root/upstream/odl-aaa-moon/aaa/aaa-authn-store/src/main/java/org/opendaylight/aaa/store/DefaultTokenStore.java
blob: df65be3284cb9e05f5293ab035950e4422be6e17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (c) 2014, 2015 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.aaa.store;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.concurrent.locks.ReentrantLock;
import javax.management.MBeanServer;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.management.ManagementService;
import org.apache.felix.dm.Component;
import org.opendaylight.aaa.api.Authentication;
import org.opendaylight.aaa.api.TokenStore;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A default token store for STS.
 *
 * @author liemmn
 *
 */
public class DefaultTokenStore implements TokenStore, ManagedService {
    private static final Logger LOG = LoggerFactory.getLogger(DefaultTokenStore.class);
    private static final String TOKEN_STORE_CONFIG_ERR = "Token store configuration error";

    private static final String TOKEN_CACHE_MANAGER = "org.opendaylight.aaa";
    private static final String TOKEN_CACHE = "tokens";
    private static final String EHCACHE_XML = "etc/ehcache.xml";

    static final String MAX_CACHED_MEMORY = "maxCachedTokensInMemory";
    static final String MAX_CACHED_DISK = "maxCachedTokensOnDisk";
    static final String SECS_TO_LIVE = "secondsToLive";
    static final String SECS_TO_IDLE = "secondsToIdle";

    // Defaults (needed only for non-Karaf deployments)
    static final Dictionary<String, String> defaults = new Hashtable<>();
    static {
        defaults.put(MAX_CACHED_MEMORY, Long.toString(10000));
        defaults.put(MAX_CACHED_DISK, Long.toString(1000000));
        defaults.put(SECS_TO_IDLE, Long.toString(3600));
        defaults.put(SECS_TO_LIVE, Long.toString(3600));
    }

    // Token cache lock
    private static final ReentrantLock cacheLock = new ReentrantLock();

    // Token cache
    private Cache tokens;

    // This should be a singleton
    DefaultTokenStore() {
    }

    // Called by DM when all required dependencies are satisfied.
    void init(Component c) {
        File ehcache = new File(EHCACHE_XML);
        CacheManager cm;
        if (ehcache.exists()) {
            cm = CacheManager.create(ehcache.getAbsolutePath());
            tokens = cm.getCache(TOKEN_CACHE);
            LOG.info("Initialized token store with custom cache config");
        } else {
            cm = CacheManager.getInstance();
            tokens = new Cache(
                    new CacheConfiguration(TOKEN_CACHE,
                            Integer.parseInt(defaults.get(MAX_CACHED_MEMORY))).maxEntriesLocalDisk(
                            Integer.parseInt(defaults.get(MAX_CACHED_DISK)))
                                                                              .timeToLiveSeconds(
                                                                                      Long.parseLong(defaults.get(SECS_TO_LIVE)))
                                                                              .timeToIdleSeconds(
                                                                                      Long.parseLong(defaults.get(SECS_TO_IDLE))));
            cm.addCache(tokens);
            LOG.info("Initialized token store with default cache config");
        }
        cm.setName(TOKEN_CACHE_MANAGER);

        // JMX for cache management
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        ManagementService.registerMBeans(cm, mBeanServer, false, false, false, true);
    }

    // Called on shutdown
    void destroy() {
        LOG.info("Shutting down token store...");
        CacheManager.getInstance().shutdown();
    }

    @Override
    public Authentication get(String token) {
        Element elem = tokens.get(token);
        return (Authentication) ((elem != null) ? elem.getObjectValue() : null);
    }

    @Override
    public void put(String token, Authentication auth) {
        tokens.put(new Element(token, auth));
    }

    @Override
    public boolean delete(String token) {
        return tokens.remove(token);
    }

    @Override
    public long tokenExpiration() {
        return tokens.getCacheConfiguration().getTimeToLiveSeconds();
    }

    @Override
    public void updated(@SuppressWarnings("rawtypes") Dictionary props)
            throws ConfigurationException {
        LOG.info("Updating token store configuration...");
        if (props == null) {
            // Someone deleted the configuration, use defaults
            props = defaults;
        }
        reconfig(props);
    }

    // Refresh cache configuration...
    private void reconfig(@SuppressWarnings("rawtypes") Dictionary props)
            throws ConfigurationException {
        cacheLock.lock();
        try {
            long secsToIdle = Long.parseLong(props.get(SECS_TO_IDLE).toString());
            long secsToLive = Long.parseLong(props.get(SECS_TO_LIVE).toString());
            int maxMem = Integer.parseInt(props.get(MAX_CACHED_MEMORY).toString());
            int maxDisk = Integer.parseInt(props.get(MAX_CACHED_DISK).toString());
            CacheConfiguration config = tokens.getCacheConfiguration();
            config.setTimeToIdleSeconds(secsToIdle);
            config.setTimeToLiveSeconds(secsToLive);
            config.maxEntriesLocalHeap(maxMem);
            config.maxEntriesLocalDisk(maxDisk);
        } catch (Throwable t) {
            throw new ConfigurationException(null, TOKEN_STORE_CONFIG_ERR, t);
        } finally {
            cacheLock.unlock();
        }
    }
}