|
| 1 | +/* |
| 2 | + * This file is part of Hopsworks |
| 3 | + * Copyright (C) 2023, Hopsworks AB. All rights reserved |
| 4 | + * |
| 5 | + * Hopsworks is free software: you can redistribute it and/or modify it under the terms of |
| 6 | + * the GNU Affero General Public License as published by the Free Software Foundation, |
| 7 | + * either version 3 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * Hopsworks is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 10 | + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 11 | + * PURPOSE. See the GNU Affero General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU Affero General Public License along with this program. |
| 14 | + * If not, see <https://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | +package io.hops.hopsworks.common.jupyter; |
| 17 | + |
| 18 | +import com.hazelcast.core.HazelcastInstance; |
| 19 | +import com.hazelcast.map.IMap; |
| 20 | +import com.hazelcast.query.Predicate; |
| 21 | +import com.hazelcast.query.Predicates; |
| 22 | +import io.hops.hopsworks.common.util.DateUtils; |
| 23 | + |
| 24 | +import javax.ejb.Singleton; |
| 25 | +import javax.ejb.TransactionAttribute; |
| 26 | +import javax.ejb.TransactionAttributeType; |
| 27 | +import javax.inject.Inject; |
| 28 | +import java.nio.file.Paths; |
| 29 | +import java.util.Collection; |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.Iterator; |
| 32 | +import java.util.Optional; |
| 33 | +import java.util.Set; |
| 34 | +import java.util.TreeSet; |
| 35 | + |
| 36 | +@Singleton |
| 37 | +@TransactionAttribute(TransactionAttributeType.NEVER) |
| 38 | +public class JupyterJWTCache { |
| 39 | + private static final String MAP_NAME = "jupyterJWTMap"; |
| 40 | + @Inject |
| 41 | + private HazelcastInstance hazelcastInstance; |
| 42 | + |
| 43 | + private final TreeSet<JupyterJWTDTO> jupyterJWTs = new TreeSet<>((t0, t1) -> { |
| 44 | + if (t0.equals(t1)) { |
| 45 | + return 0; |
| 46 | + } else { |
| 47 | + if (t0.getExpiration().isBefore(t1.getExpiration())) { |
| 48 | + return -1; |
| 49 | + } else if (t0.getExpiration().isAfter(t1.getExpiration())) { |
| 50 | + return 1; |
| 51 | + } |
| 52 | + return 0; |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + private final HashMap<CidAndPort, JupyterJWT> pidAndPortToJWT = new HashMap<>(); |
| 57 | + |
| 58 | + public void add(JupyterJWT jupyterJWT) { |
| 59 | + if (hazelcastInstance != null) { |
| 60 | + IMap<CidAndPort, JupyterJWTDTO> pidAndPortToJWTMap = hazelcastInstance.getMap(MAP_NAME); |
| 61 | + pidAndPortToJWTMap.put(jupyterJWT.pidAndPort, new JupyterJWTDTO(jupyterJWT)); |
| 62 | + } else { |
| 63 | + jupyterJWTs.add(new JupyterJWTDTO(jupyterJWT)); |
| 64 | + pidAndPortToJWT.put(jupyterJWT.pidAndPort, jupyterJWT); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public Optional<JupyterJWT> get(CidAndPort pidAndPort) { |
| 69 | + if (hazelcastInstance != null) { |
| 70 | + IMap<CidAndPort, JupyterJWTDTO> pidAndPortToJWTMap = hazelcastInstance.getMap(MAP_NAME); |
| 71 | + JupyterJWTDTO jupyterJWTDTO = pidAndPortToJWTMap.get(pidAndPort); |
| 72 | + if (jupyterJWTDTO != null) { |
| 73 | + return Optional.of( |
| 74 | + new JupyterJWT(jupyterJWTDTO.getProject(), jupyterJWTDTO.getUser(), jupyterJWTDTO.getExpiration(), |
| 75 | + pidAndPort, jupyterJWTDTO.getToken(), Paths.get(jupyterJWTDTO.getTokenFile()))); |
| 76 | + } |
| 77 | + return Optional.empty(); |
| 78 | + } else { |
| 79 | + return Optional.ofNullable(pidAndPortToJWT.get(pidAndPort)); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public void remove(CidAndPort pidAndPort) { |
| 84 | + if (hazelcastInstance != null) { |
| 85 | + IMap<CidAndPort, JupyterJWTDTO> pidAndPortToJWTMap = hazelcastInstance.getMap(MAP_NAME); |
| 86 | + pidAndPortToJWTMap.remove(pidAndPort); |
| 87 | + } else { |
| 88 | + JupyterJWT jupyterJWT = pidAndPortToJWT.remove(pidAndPort); |
| 89 | + jupyterJWTs.remove(new JupyterJWTDTO(jupyterJWT)); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public void replaceAll(Set<JupyterJWT> renewedJWTs) { |
| 94 | + if (hazelcastInstance != null) { |
| 95 | + IMap<CidAndPort, JupyterJWTDTO> pidAndPortToJWTMap = hazelcastInstance.getMap(MAP_NAME); |
| 96 | + renewedJWTs.forEach(t -> pidAndPortToJWTMap.replace(t.pidAndPort, new JupyterJWTDTO(t))); |
| 97 | + } else { |
| 98 | + renewedJWTs.forEach(t -> { |
| 99 | + //remove old token |
| 100 | + JupyterJWT jupyterJWT = pidAndPortToJWT.remove(t.pidAndPort); |
| 101 | + jupyterJWTs.remove(new JupyterJWTDTO(jupyterJWT)); |
| 102 | + //Add the new token |
| 103 | + jupyterJWTs.add(new JupyterJWTDTO(t)); |
| 104 | + pidAndPortToJWT.put(t.pidAndPort, t); |
| 105 | + }); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public int getSize() { |
| 110 | + if (hazelcastInstance != null) { |
| 111 | + IMap<CidAndPort, JupyterJWTDTO> pidAndPortToJWTMap = hazelcastInstance.getMap(MAP_NAME); |
| 112 | + return pidAndPortToJWTMap.size(); |
| 113 | + } else { |
| 114 | + return jupyterJWTs.size(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public Iterator<JupyterJWTDTO> getMaybeExpired() { |
| 119 | + if (hazelcastInstance != null) { |
| 120 | + IMap<CidAndPort, JupyterJWTDTO> pidAndPortToJWTMap = hazelcastInstance.getMap(MAP_NAME); |
| 121 | + Predicate<CidAndPort, JupyterJWTDTO> expirationPredicate = Predicates.lessEqual("expiration", DateUtils.getNow()); |
| 122 | + Collection<JupyterJWTDTO> jupyterJWTDTOS = pidAndPortToJWTMap.values(expirationPredicate); |
| 123 | + return jupyterJWTDTOS.iterator(); |
| 124 | + } else { |
| 125 | + return jupyterJWTs.iterator(); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments