summaryrefslogtreecommitdiff
path: root/adservices/service-core/java/com/android/adservices/service/measurement/aggregation/AggregateEncryptionKeyManager.java
blob: 292002d8fb7b086d64532425b978cc16d2acb625 (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
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.adservices.service.measurement.aggregation;

import android.net.Uri;

import com.android.adservices.LogUtil;
import com.android.adservices.data.measurement.DatastoreManager;
import com.android.adservices.service.AdServicesConfig;
import com.android.internal.annotations.VisibleForTesting;

import java.time.Clock;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;

/** A public key used to encrypt aggregatable reports. */
public final class AggregateEncryptionKeyManager {
    private final DatastoreManager mDatastoreManager;
    private final AggregateEncryptionKeyFetcher mAggregateEncryptionKeyFetcher;
    private final Clock mClock;
    private final Uri mAggregateEncryptionKeyCoordinatorUrl;

    public AggregateEncryptionKeyManager(DatastoreManager datastoreManager) {
        mDatastoreManager = datastoreManager;
        mAggregateEncryptionKeyFetcher = new AggregateEncryptionKeyFetcher();
        mClock = Clock.systemUTC();
        mAggregateEncryptionKeyCoordinatorUrl =
                Uri.parse(AdServicesConfig.getMeasurementAggregateEncryptionKeyCoordinatorUrl());
    }

    @VisibleForTesting
    AggregateEncryptionKeyManager(DatastoreManager datastoreManager,
            AggregateEncryptionKeyFetcher aggregateEncryptionKeyFetcher,
            Clock clock,
            Uri aggregateEncryptionKeyCoordinatorUrl) {
        mDatastoreManager = datastoreManager;
        mAggregateEncryptionKeyFetcher = aggregateEncryptionKeyFetcher;
        mClock = clock;
        mAggregateEncryptionKeyCoordinatorUrl = aggregateEncryptionKeyCoordinatorUrl;
    }

    /**
     * Retrieves a {@link List<AggregateEncryptionKey>} in which the size of the collection matches
     * the numKeys specified in the parameters. If no keys are found, the collection would be empty.
     */
    public List<AggregateEncryptionKey> getAggregateEncryptionKeys(int numKeys) {
        long eventTime = mClock.millis();

        Optional<List<AggregateEncryptionKey>> aggregateEncryptionKeysOptional =
                mDatastoreManager.runInTransactionWithResult((dao) ->
                        dao.getNonExpiredAggregateEncryptionKeys(eventTime));

        List<AggregateEncryptionKey> aggregateEncryptionKeys =
                aggregateEncryptionKeysOptional.orElse(new ArrayList<>());

        // If no non-expired keys are available (or the datastore retrieval failed), fetch them
        // over the network, insert them in the datastore and delete expired keys.
        if (aggregateEncryptionKeys.size() == 0) {
            Optional<List<AggregateEncryptionKey>> fetchResult =
                    mAggregateEncryptionKeyFetcher.fetch(
                            mAggregateEncryptionKeyCoordinatorUrl, eventTime);
            if (fetchResult.isPresent()) {
                aggregateEncryptionKeys = fetchResult.get();
                for (AggregateEncryptionKey aggregateEncryptionKey : aggregateEncryptionKeys) {
                    mDatastoreManager.runInTransaction((dao) ->
                            dao.insertAggregateEncryptionKey(aggregateEncryptionKey));
                }
                mDatastoreManager.runInTransaction((dao) ->
                        dao.deleteExpiredAggregateEncryptionKeys(eventTime));
            } else {
                LogUtil.d("Fetching aggregate encryption keys over the network failed.");
            }
        }

        return getRandomListOfKeys(aggregateEncryptionKeys, numKeys);
    }

    @VisibleForTesting
    List<AggregateEncryptionKey> getRandomListOfKeys(
            List<AggregateEncryptionKey> aggregateEncryptionKeys, int numKeys) {
        List<AggregateEncryptionKey> result = new ArrayList<>();
        Random random = new Random();
        int numAvailableKeys = aggregateEncryptionKeys.size();
        if (numAvailableKeys > 0) {
            for (int i = 0; i < numKeys; i++) {
                int index = random.nextInt(numAvailableKeys);
                result.add(aggregateEncryptionKeys.get(index));
            }
        }
        return result;
    }
}