summaryrefslogtreecommitdiffstats
path: root/toolkit/components/normandy/actions/AddonRolloutAction.sys.mjs
blob: 003bd31fffef6d85d1d388a95e97cf85954bb119 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { BaseAction } from "resource://normandy/actions/BaseAction.sys.mjs";

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  ActionSchemas: "resource://normandy/actions/schemas/index.sys.mjs",
  AddonRollouts: "resource://normandy/lib/AddonRollouts.sys.mjs",
  NormandyAddonManager: "resource://normandy/lib/NormandyAddonManager.sys.mjs",
  NormandyApi: "resource://normandy/lib/NormandyApi.sys.mjs",
  TelemetryEnvironment: "resource://gre/modules/TelemetryEnvironment.sys.mjs",
  TelemetryEvents: "resource://normandy/lib/TelemetryEvents.sys.mjs",
});

class AddonRolloutError extends Error {
  /**
   * @param {string} slug
   * @param {object} extra Extra details to include when reporting the error to telemetry.
   * @param {string} extra.reason The specific reason for the failure.
   */
  constructor(slug, extra) {
    let message;
    let { reason } = extra;
    switch (reason) {
      case "conflict": {
        message = "an existing rollout already exists for this add-on";
        break;
      }
      case "addon-id-changed": {
        message = "upgrade add-on ID does not match installed add-on ID";
        break;
      }
      case "upgrade-required": {
        message = "a newer version of the add-on is already installed";
        break;
      }
      case "download-failure": {
        message = "the add-on failed to download";
        break;
      }
      case "metadata-mismatch": {
        message = "the server metadata does not match the downloaded add-on";
        break;
      }
      case "install-failure": {
        message = "the add-on failed to install";
        break;
      }
      default: {
        throw new Error(`Unexpected AddonRolloutError reason: ${reason}`);
      }
    }
    super(`Cannot install add-on for rollout (${slug}): ${message}.`);
    this.slug = slug;
    this.extra = extra;
  }
}

export class AddonRolloutAction extends BaseAction {
  get schema() {
    return lazy.ActionSchemas["addon-rollout"];
  }

  async _run(recipe) {
    const { extensionApiId, slug } = recipe.arguments;

    const existingRollout = await lazy.AddonRollouts.get(slug);
    const eventName = existingRollout ? "update" : "enroll";
    const extensionDetails = await lazy.NormandyApi.fetchExtensionDetails(
      extensionApiId
    );

    // Check if the existing rollout matches the current rollout
    if (
      existingRollout &&
      existingRollout.addonId === extensionDetails.extension_id
    ) {
      const versionCompare = Services.vc.compare(
        existingRollout.addonVersion,
        extensionDetails.version
      );

      if (versionCompare === 0) {
        return; // Do nothing
      }
    }

    const createError = (reason, extra = {}) => {
      return new AddonRolloutError(slug, {
        ...extra,
        reason,
      });
    };

    // Check for a conflict (addon already installed by another rollout)
    const activeRollouts = await lazy.AddonRollouts.getAllActive();
    const conflictingRollout = activeRollouts.find(
      rollout =>
        rollout.slug !== slug &&
        rollout.addonId === extensionDetails.extension_id
    );
    if (conflictingRollout) {
      const conflictError = createError("conflict", {
        addonId: conflictingRollout.addonId,
        conflictingSlug: conflictingRollout.slug,
      });
      this.reportError(conflictError, "enrollFailed");
      throw conflictError;
    }

    const onInstallStarted = (install, installDeferred) => {
      const existingAddon = install.existingAddon;

      if (existingRollout && existingRollout.addonId !== install.addon.id) {
        installDeferred.reject(createError("addon-id-changed"));
        return false; // cancel the upgrade, the add-on ID has changed
      }

      if (
        existingAddon &&
        Services.vc.compare(existingAddon.version, install.addon.version) > 0
      ) {
        installDeferred.reject(createError("upgrade-required"));
        return false; // cancel the installation, must be an upgrade
      }

      return true;
    };

    const applyNormandyChanges = async install => {
      const details = {
        addonId: install.addon.id,
        addonVersion: install.addon.version,
        extensionApiId,
        xpiUrl: extensionDetails.xpi,
        xpiHash: extensionDetails.hash,
        xpiHashAlgorithm: extensionDetails.hash_algorithm,
      };

      if (existingRollout) {
        await lazy.AddonRollouts.update({
          ...existingRollout,
          ...details,
        });
      } else {
        await lazy.AddonRollouts.add({
          recipeId: recipe.id,
          state: lazy.AddonRollouts.STATE_ACTIVE,
          slug,
          ...details,
        });
      }
    };

    const undoNormandyChanges = async () => {
      if (existingRollout) {
        await lazy.AddonRollouts.update(existingRollout);
      } else {
        await lazy.AddonRollouts.delete(recipe.id);
      }
    };

    const [installedId, installedVersion] =
      await lazy.NormandyAddonManager.downloadAndInstall({
        createError,
        extensionDetails,
        applyNormandyChanges,
        undoNormandyChanges,
        onInstallStarted,
        reportError: error => this.reportError(error, `${eventName}Failed`),
      });

    if (existingRollout) {
      this.log.debug(`Updated addon rollout ${slug}`);
    } else {
      this.log.debug(`Enrolled in addon rollout ${slug}`);
      lazy.TelemetryEnvironment.setExperimentActive(
        slug,
        lazy.AddonRollouts.STATE_ACTIVE,
        {
          type: "normandy-addonrollout",
        }
      );
    }

    // All done, report success to Telemetry
    lazy.TelemetryEvents.sendEvent(eventName, "addon_rollout", slug, {
      addonId: installedId,
      addonVersion: installedVersion,
    });
  }

  reportError(error, eventName) {
    if (error instanceof AddonRolloutError) {
      // One of our known errors. Report it nicely to telemetry
      lazy.TelemetryEvents.sendEvent(
        eventName,
        "addon_rollout",
        error.slug,
        error.extra
      );
    } else {
      /*
       * Some unknown error. Add some helpful details, and report it to
       * telemetry. The actual stack trace and error message could possibly
       * contain PII, so we don't include them here. Instead include some
       * information that should still be helpful, and is less likely to be
       * unsafe.
       */
      const safeErrorMessage = `${error.fileName}:${error.lineNumber}:${error.columnNumber} ${error.name}`;
      lazy.TelemetryEvents.sendEvent(eventName, "addon_rollout", error.slug, {
        reason: safeErrorMessage.slice(0, 80), // max length is 80 chars
      });
    }
  }
}