|
34 | 34 | using util::StatusOr;
|
35 | 35 | using util::StringFormat;
|
36 | 36 |
|
37 |
| -std::string LoadGrpcRootCertificate() { |
38 |
| - // Try to load certificates bundled by gRPC-C++ if available (depends on |
39 |
| - // gRPC-C++ version). |
40 |
| - // Note that `mainBundle` may be nil in certain cases (e.g., unit tests). |
41 |
| - NSBundle* bundle = [NSBundle bundleWithIdentifier:@"org.cocoapods.grpcpp"]; |
42 |
| - NSString* path; |
43 |
| - if (bundle) { |
44 |
| - path = |
45 |
| - [bundle pathForResource:@"gRPCCertificates.bundle/roots" ofType:@"pem"]; |
46 |
| - } |
47 |
| - if (path) { |
48 |
| - LOG_DEBUG("Using roots.pem file from gRPC-C++ pod"); |
49 |
| - } else { |
| 37 | +NSString* FindPathToCertificatesFile() { |
| 38 | + // Certificates file might be present in one of several bundles, based on |
| 39 | + // the environment. |
| 40 | + NSArray<NSBundle*>* bundles = @[ |
| 41 | + // First, try to load certificates bundled by gRPC-C++ if available |
| 42 | + // (pod versions 0.0.6+). |
| 43 | + [NSBundle bundleWithIdentifier:@"org.cocoapods.grpcpp"], |
50 | 44 | // Fall back to the certificates bundled with Firestore if necessary.
|
51 |
| - LOG_DEBUG("Using roots.pem file from Firestore pod"); |
| 45 | + [NSBundle bundleForClass:FSTFirestoreClient.class], |
| 46 | + // Finally, users manually adding resources to the project may add the |
| 47 | + // certificate to the main application bundle. Note that `mainBundle` is nil |
| 48 | + // for unit tests of library projects, so it cannot fully substitute for |
| 49 | + // checking framework bundles. |
| 50 | + [NSBundle mainBundle], |
| 51 | + ]; |
| 52 | + |
| 53 | + for (NSBundle* bundle in bundles) { |
| 54 | + if (!bundle) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + NSString* resource = @"gRPCCertificates.bundle/roots"; |
| 59 | + NSString* path = [bundle pathForResource:resource ofType:@"pem"]; |
| 60 | + if (!path) { |
| 61 | + resource = @"gRPCCertificates-Firestore.bundle/roots"; |
| 62 | + path = [bundle pathForResource:resource ofType:@"pem"]; |
| 63 | + } |
52 | 64 |
|
53 |
| - bundle = [NSBundle bundleForClass:FSTFirestoreClient.class]; |
54 |
| - HARD_ASSERT(bundle, "Could not find Firestore bundle"); |
55 |
| - path = [bundle pathForResource:@"gRPCCertificates-Firestore.bundle/roots" |
56 |
| - ofType:@"pem"]; |
| 65 | + if (path) { |
| 66 | + LOG_DEBUG("%s.pem found in bundle %s", resource, |
| 67 | + [bundle bundleIdentifier]); |
| 68 | + return path; |
| 69 | + } else { |
| 70 | + LOG_DEBUG("%s.pem not found in bundle %s", resource, |
| 71 | + [bundle bundleIdentifier]); |
| 72 | + } |
57 | 73 | }
|
58 | 74 |
|
| 75 | + return nil; |
| 76 | +} |
| 77 | + |
| 78 | +std::string LoadGrpcRootCertificate() { |
| 79 | + NSString* path = FindPathToCertificatesFile(); |
59 | 80 | HARD_ASSERT(
|
60 | 81 | path,
|
61 | 82 | "Could not load root certificates from the bundle. SSL cannot work.");
|
|
0 commit comments