1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.adservices.data.adselection; 18 19 import androidx.room.Dao; 20 import androidx.room.Insert; 21 import androidx.room.OnConflictStrategy; 22 import androidx.room.Query; 23 24 import java.time.Instant; 25 import java.util.List; 26 27 /** Dao to manage access to entities in the Auction Server Encryption Key table. */ 28 @Dao 29 public abstract class EncryptionKeyDao { 30 /** 31 * Returns the EncryptionKey of given key type with the latest expiry instant. 32 * 33 * @param encryptionKeyType Type of Key to query 34 * @return Returns EncryptionKey with latest expiry instant. 35 */ 36 @Query( 37 "SELECT * FROM encryption_key " 38 + "WHERE encryption_key_type = :encryptionKeyType " 39 + "ORDER BY expiry_instant DESC " 40 + "LIMIT :count ") getLatestExpiryNKeysOfType( @ncryptionKeyConstants.EncryptionKeyType int encryptionKeyType, int count)41 public abstract List<DBEncryptionKey> getLatestExpiryNKeysOfType( 42 @EncryptionKeyConstants.EncryptionKeyType int encryptionKeyType, int count); 43 44 /** 45 * Fetches N number of non-expired EncryptionKey of given key type. 46 * 47 * @param encryptionKeyType Type of EncryptionKey to Query 48 * @param now expiry Instant should be greater than this given instant. 49 * @param count Number of keys to return. 50 * @return 51 */ 52 @Query( 53 "SELECT * FROM encryption_key " 54 + "WHERE encryption_key_type = :encryptionKeyType " 55 + "AND expiry_instant >= :now " 56 + "ORDER BY expiry_instant DESC " 57 + "LIMIT :count ") getLatestExpiryNActiveKeysOfType( @ncryptionKeyConstants.EncryptionKeyType int encryptionKeyType, Instant now, int count)58 public abstract List<DBEncryptionKey> getLatestExpiryNActiveKeysOfType( 59 @EncryptionKeyConstants.EncryptionKeyType int encryptionKeyType, 60 Instant now, 61 int count); 62 63 /** 64 * Fetches expired keys of given key type. A key is considered expired with its expiryInstant is 65 * lower than the given instant. 66 * 67 * @param type Type of EncryptionKey to Query. 68 * @param now Upper bound instant for expiry determination. 69 * @return Returns expired keys of given key type. 70 */ 71 @Query( 72 "SELECT * " 73 + " FROM encryption_key " 74 + "WHERE expiry_instant < :now AND " 75 + "encryption_key_type = :type") getExpiredKeysForType( @ncryptionKeyConstants.EncryptionKeyType int type, Instant now)76 public abstract List<DBEncryptionKey> getExpiredKeysForType( 77 @EncryptionKeyConstants.EncryptionKeyType int type, Instant now); 78 79 /** 80 * Returns expired keys in the table. 81 * 82 * @param now A keys is considered expired if key's expiryInstant is lower than this given 83 * instant. 84 * @return Returns expired keys keyed by key type. 85 */ 86 @Query("SELECT * FROM encryption_key " + "WHERE expiry_instant < :now ") getExpiredKeys(Instant now)87 public abstract List<DBEncryptionKey> getExpiredKeys(Instant now); 88 89 /** Deletes expired keys of the given encryption key type. */ 90 @Query("DELETE FROM encryption_key WHERE expiry_instant < :now AND encryption_key_type = :type") deleteExpiredRowsByType( @ncryptionKeyConstants.EncryptionKeyType int type, Instant now)91 public abstract int deleteExpiredRowsByType( 92 @EncryptionKeyConstants.EncryptionKeyType int type, Instant now); 93 94 /** Delete all keys from the table. */ 95 @Query("DELETE FROM encryption_key") deleteAllEncryptionKeys()96 public abstract int deleteAllEncryptionKeys(); 97 98 /** Insert into the table all the given EncryptionKeys. */ 99 @Insert(onConflict = OnConflictStrategy.REPLACE) insertAllKeys(List<DBEncryptionKey> keys)100 public abstract void insertAllKeys(List<DBEncryptionKey> keys); 101 } 102