|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2025 Google LLC | 
|  | 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 | +import ArgumentParser | 
|  | 18 | +import Foundation | 
|  | 19 | +import Logging | 
|  | 20 | +import Util | 
|  | 21 | + | 
|  | 22 | +extension Tests { | 
|  | 23 | +  /// Command for decrypting the secret files needed for a test run. | 
|  | 24 | +  struct Decrypt: ParsableCommand { | 
|  | 25 | +    nonisolated(unsafe) static var configuration = CommandConfiguration( | 
|  | 26 | +      abstract: "Decrypt the secret files for a test run.", | 
|  | 27 | +      usage: """ | 
|  | 28 | +        tests decrypt [--json] [--overwrite] [<json-file>] | 
|  | 29 | +        tests decrypt [--password <password>] [--overwrite] [<secret-files> ...] | 
|  | 30 | +
 | 
|  | 31 | +        tests decrypt --json secret_files.json | 
|  | 32 | +        tests decrypt --json --overwrite secret_files.json | 
|  | 33 | +        tests decrypt --password "super_secret" \\ | 
|  | 34 | +          scripts/gha-encrypted/FirebaseAI/TestApp-GoogleService-Info.plist.gpg:FirebaseAI/Tests/TestApp/Resources/GoogleService-Info.plist \\ | 
|  | 35 | +          scripts/gha-encrypted/FirebaseAI/TestApp-GoogleService-Info-Spark.plist.gpg:FirebaseAI/Tests/TestApp/Resources/GoogleService-Info-Spark.plist | 
|  | 36 | +      """, | 
|  | 37 | +      discussion: """ | 
|  | 38 | +        The happy path usage is saving the secret passphrase in the environment variable \ | 
|  | 39 | +      'secrets_passphrase', and passing a json file to the command. Although, you can also \ | 
|  | 40 | +      pass everything inline via options. | 
|  | 41 | +
 | 
|  | 42 | +        When using a json file, it's expected that the json file is an array of json elements \ | 
|  | 43 | +      in the format of: | 
|  | 44 | +        { encrypted: <path-to-encrypted-file>, destination: <where-to-output-decrypted-file> } | 
|  | 45 | +      """, | 
|  | 46 | +    ) | 
|  | 47 | + | 
|  | 48 | +    @Argument( | 
|  | 49 | +      help: """ | 
|  | 50 | +      An array of secret files to decrypt. \ | 
|  | 51 | +      The files should be in the format "encrypted:destination", where "encrypted" is a path to \ | 
|  | 52 | +      the encrypted file and "destination" is a path to where the decrypted file should be saved. | 
|  | 53 | +      """ | 
|  | 54 | +    ) | 
|  | 55 | +    var secretFiles: [String] = [] | 
|  | 56 | + | 
|  | 57 | +    @Option( | 
|  | 58 | +      help: """ | 
|  | 59 | +      The secret to use when decrypting the files. \ | 
|  | 60 | +      Defaults to the environment variable 'secrets_passphrase'. | 
|  | 61 | +      """ | 
|  | 62 | +    ) | 
|  | 63 | +    var password: String = "" | 
|  | 64 | + | 
|  | 65 | +    @Flag(help: "Overwrite existing decrypted secret files.") | 
|  | 66 | +    var overwrite: Bool = false | 
|  | 67 | + | 
|  | 68 | +    @Flag( | 
|  | 69 | +      help: """ | 
|  | 70 | +      Use a json file of secret file mappings instead. \ | 
|  | 71 | +      When this flag is enabled, <secret-files> should be a single json file. | 
|  | 72 | +      """ | 
|  | 73 | +    ) | 
|  | 74 | +    var json: Bool = false | 
|  | 75 | + | 
|  | 76 | +    /// The parsed version of ``secretFiles``. | 
|  | 77 | +    /// | 
|  | 78 | +    /// Only populated after `validate()` runs. | 
|  | 79 | +    var files: [SecretFile] = [] | 
|  | 80 | + | 
|  | 81 | +    static let log = Logger(label: "Tests::Decrypt") | 
|  | 82 | +    private var log: Logger { Decrypt.log } | 
|  | 83 | + | 
|  | 84 | +    mutating func validate() throws { | 
|  | 85 | +      try validatePassword() | 
|  | 86 | + | 
|  | 87 | +      if json { | 
|  | 88 | +        try validateJSON() | 
|  | 89 | +      } else { | 
|  | 90 | +        try validateFileString() | 
|  | 91 | +      } | 
|  | 92 | + | 
|  | 93 | +      if !overwrite { | 
|  | 94 | +        log.info("Overwrite is disabled, so we're skipping generation for existing files.") | 
|  | 95 | +        files = files.filter { file in | 
|  | 96 | +          let keep = !FileManager.default.fileExists(atPath: file.destination) | 
|  | 97 | +          if !keep { | 
|  | 98 | +            log.debug( | 
|  | 99 | +              "Skipping generation for existing file", | 
|  | 100 | +              metadata: ["destination": "\(file.destination)"] | 
|  | 101 | +            ) | 
|  | 102 | +          } | 
|  | 103 | +          return keep | 
|  | 104 | +        } | 
|  | 105 | +      } | 
|  | 106 | + | 
|  | 107 | +      for file in files { | 
|  | 108 | +        guard FileManager.default.fileExists(atPath: file.encrypted) else { | 
|  | 109 | +          throw ValidationError("Encrypted secret file does not exist: \(file.encrypted)") | 
|  | 110 | +        } | 
|  | 111 | +      } | 
|  | 112 | +    } | 
|  | 113 | + | 
|  | 114 | +    private mutating func validatePassword() throws { | 
|  | 115 | +      if password.isEmpty { | 
|  | 116 | +        // when a password isn't provided, try to load one from the environment variable | 
|  | 117 | +        guard | 
|  | 118 | +          let secrets_passphrase = ProcessInfo.processInfo.environment["secrets_passphrase"] | 
|  | 119 | +        else { | 
|  | 120 | +          throw ValidationError( | 
|  | 121 | +            "Either provide a passphrase via the password option or set the environvment variable 'secrets_passphrase' to the passphrase." | 
|  | 122 | +          ) | 
|  | 123 | +        } | 
|  | 124 | +        password = secrets_passphrase | 
|  | 125 | +      } | 
|  | 126 | +    } | 
|  | 127 | + | 
|  | 128 | +    private mutating func validateJSON() throws { | 
|  | 129 | +      guard let jsonPath = secretFiles.first else { | 
|  | 130 | +        throw ValidationError("Missing path to json file for secret files") | 
|  | 131 | +      } | 
|  | 132 | + | 
|  | 133 | +      let fileURL = URL( | 
|  | 134 | +        filePath: jsonPath, directoryHint: .notDirectory, | 
|  | 135 | +        relativeTo: URL.currentDirectory() | 
|  | 136 | +      ) | 
|  | 137 | + | 
|  | 138 | +      files = try SecretFile.parseArrayFrom(file: fileURL) | 
|  | 139 | +      guard !files.isEmpty else { | 
|  | 140 | +        throw ValidationError("Missing secret files in json file: \(jsonPath)") | 
|  | 141 | +      } | 
|  | 142 | +    } | 
|  | 143 | + | 
|  | 144 | +    private mutating func validateFileString() throws { | 
|  | 145 | +      guard !secretFiles.isEmpty else { | 
|  | 146 | +        throw ValidationError("Missing paths to secret files") | 
|  | 147 | +      } | 
|  | 148 | +      for string in secretFiles { | 
|  | 149 | +        try files.append(SecretFile(string: string)) | 
|  | 150 | +      } | 
|  | 151 | +    } | 
|  | 152 | + | 
|  | 153 | +    mutating func run() throws { | 
|  | 154 | +      log.info("Decrypting files...") | 
|  | 155 | + | 
|  | 156 | +      for file in files { | 
|  | 157 | +        let gpg = Process("gpg", inheritEnvironment: true) | 
|  | 158 | +        let result = try gpg.runWithSignals([ | 
|  | 159 | +          "--quiet", | 
|  | 160 | +          "--batch", | 
|  | 161 | +          "--yes", | 
|  | 162 | +          "--decrypt", | 
|  | 163 | +          "--passphrase=\(password)", | 
|  | 164 | +          "--output", | 
|  | 165 | +          file.destination, | 
|  | 166 | +          file.encrypted, | 
|  | 167 | +        ]) | 
|  | 168 | + | 
|  | 169 | +        guard result == 0 else { | 
|  | 170 | +          log.error("Failed to decrypt file", metadata: ["file": "\(file.encrypted)"]) | 
|  | 171 | +          throw ExitCode(result) | 
|  | 172 | +        } | 
|  | 173 | + | 
|  | 174 | +        log.debug( | 
|  | 175 | +          "File encrypted", | 
|  | 176 | +          metadata: ["file": "\(file.encrypted)", "destination": "\(file.destination)"] | 
|  | 177 | +        ) | 
|  | 178 | +      } | 
|  | 179 | + | 
|  | 180 | +      log.info("Files decrypted") | 
|  | 181 | +    } | 
|  | 182 | +  } | 
|  | 183 | +} | 
0 commit comments