1 /* 2 * Copyright (C) 2024 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 28 public abstract class ProtectedServersEncryptionConfigDao { 29 /** 30 * Returns the EncryptionKey of given key type and fetched from the given coordinator url with 31 * 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 protected_servers_encryption_config " 38 + "WHERE encryption_key_type = :encryptionKeyType " 39 + "AND coordinator_url = :coordinatorUrl " 40 + "ORDER BY expiry_instant DESC " 41 + "LIMIT :count ") getLatestExpiryNKeys( @ncryptionKeyConstants.EncryptionKeyType int encryptionKeyType, String coordinatorUrl, int count)42 public abstract List<DBProtectedServersEncryptionConfig> getLatestExpiryNKeys( 43 @EncryptionKeyConstants.EncryptionKeyType int encryptionKeyType, 44 String coordinatorUrl, 45 int count); 46 47 /** 48 * Returns the EncryptionKey of given key type and fetched from the given coordinator url with 49 * the latest expiry instant. 50 * 51 * @param encryptionKeyType Type of Key to query 52 * @return Returns EncryptionKey with latest expiry instant. 53 */ 54 @Query( 55 "SELECT * FROM protected_servers_encryption_config " 56 + "WHERE encryption_key_type = :encryptionKeyType " 57 + "ORDER BY expiry_instant DESC " 58 + "LIMIT :count ") getLatestExpiryNKeysByType( @ncryptionKeyConstants.EncryptionKeyType int encryptionKeyType, int count)59 public abstract List<DBProtectedServersEncryptionConfig> getLatestExpiryNKeysByType( 60 @EncryptionKeyConstants.EncryptionKeyType int encryptionKeyType, int count); 61 62 /** 63 * Fetches N number of non-expired EncryptionKey of given key type and given coordinator url. 64 * 65 * @param encryptionKeyType Type of EncryptionKey to Query 66 * @param coordinatorUrl The coordinator url from which the key was fetched 67 * @param now expiry Instant should be greater than this given instant. 68 * @param count Number of keys to return. 69 * @return 70 */ 71 @Query( 72 "SELECT * FROM protected_servers_encryption_config " 73 + "WHERE encryption_key_type = :encryptionKeyType " 74 + "AND expiry_instant >= :now " 75 + "AND coordinator_url = :coordinatorUrl " 76 + "ORDER BY expiry_instant DESC " 77 + "LIMIT :count ") getLatestExpiryNActiveKeys( @ncryptionKeyConstants.EncryptionKeyType int encryptionKeyType, String coordinatorUrl, Instant now, int count)78 public abstract List<DBProtectedServersEncryptionConfig> getLatestExpiryNActiveKeys( 79 @EncryptionKeyConstants.EncryptionKeyType int encryptionKeyType, 80 String coordinatorUrl, 81 Instant now, 82 int count); 83 84 /** 85 * Fetches expired keys of given key type and coordinator URL. A key is considered expired with 86 * its expiryInstant is lower than the given instant. 87 * 88 * @param type Type of EncryptionKey to Query. 89 * @param coordinatorUrl The coordinator url from which the key was fetched 90 * @param now Upper bound instant for expiry determination. 91 * @return Returns expired keys of given key type. 92 */ 93 @Query( 94 "SELECT * " 95 + " FROM protected_servers_encryption_config " 96 + "WHERE expiry_instant < :now " 97 + "AND encryption_key_type = :type " 98 + "AND coordinator_url = :coordinatorUrl ") getExpiredKeys( @ncryptionKeyConstants.EncryptionKeyType int type, String coordinatorUrl, Instant now)99 public abstract List<DBProtectedServersEncryptionConfig> getExpiredKeys( 100 @EncryptionKeyConstants.EncryptionKeyType int type, String coordinatorUrl, Instant now); 101 102 /** 103 * Returns expired keys in the table. 104 * 105 * @param now A keys is considered expired if key's expiryInstant is lower than this given 106 * instant. 107 * @return Returns expired keys keyed by key type. 108 */ 109 @Query("SELECT * FROM protected_servers_encryption_config " + "WHERE expiry_instant < :now ") getAllExpiredKeys(Instant now)110 public abstract List<DBProtectedServersEncryptionConfig> getAllExpiredKeys(Instant now); 111 112 /** Deletes expired keys of the given encryption key type and coordinatorUrl. */ 113 @Query( 114 "DELETE FROM protected_servers_encryption_config WHERE expiry_instant < :now " 115 + "AND encryption_key_type = :type " 116 + "AND coordinator_url = :coordinatorUrl") deleteExpiredRows( @ncryptionKeyConstants.EncryptionKeyType int type, String coordinatorUrl, Instant now)117 public abstract int deleteExpiredRows( 118 @EncryptionKeyConstants.EncryptionKeyType int type, String coordinatorUrl, Instant now); 119 120 /** Delete all keys from the table. */ 121 @Query("DELETE FROM protected_servers_encryption_config") deleteAllEncryptionKeys()122 public abstract int deleteAllEncryptionKeys(); 123 124 /** Insert into the table the given protected servers encryption configs. */ 125 @Insert(onConflict = OnConflictStrategy.REPLACE) insertKeys(List<DBProtectedServersEncryptionConfig> keys)126 public abstract void insertKeys(List<DBProtectedServersEncryptionConfig> keys); 127 } 128