Skip to content

Commit

Permalink
[App Check] Add log filtering based on log level (#11570)
Browse files Browse the repository at this point in the history
Updated the logging functions in App Check to support filtering out messages based on log levels. Implementation is based on GDTCORConsoleLogger.
  • Loading branch information
andrewheard committed Jul 20, 2023
1 parent ec898f3 commit 41c3bfd
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 85 deletions.
12 changes: 6 additions & 6 deletions AppCheck/Sources/AppAttestProvider/GACAppAttestProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#import "AppCheck/Sources/AppAttestProvider/Storage/GACAppAttestKeyIDStorage.h"
#import "AppCheck/Sources/Core/APIService/GACAppCheckAPIService.h"
#import "AppCheck/Sources/Core/Backoff/GACAppCheckBackoffWrapper.h"
#import "AppCheck/Sources/Core/GACAppCheckLogger.h"
#import "AppCheck/Sources/Core/GACAppCheckLogger+Internal.h"

#import "AppCheck/Sources/Core/Utils/GACAppCheckCryptoUtils.h"

Expand Down Expand Up @@ -250,8 +250,8 @@ - (void)getTokenWithCompletion:(void (^)(GACAppCheckToken *_Nullable, NSError *_
return [self attestationState].thenOn(self.queue, ^id(GACAppAttestProviderState *attestState) {
switch (attestState.state) {
case GACAppAttestAttestationStateUnsupported:
GACLogDebug(kGACLoggerAppCheckMessageCodeAppAttestNotSupported,
@"App Attest is not supported.");
GACAppCheckLogDebug(GACLoggerAppCheckMessageCodeAppAttestNotSupported,
@"App Attest is not supported.");
return attestState.appAttestUnsupportedError;
break;

Expand Down Expand Up @@ -370,9 +370,9 @@ - (void)getTokenWithCompletion:(void (^)(GACAppCheckToken *_Nullable, NSError *_
GACAppCheckHTTPError *HTTPError = (GACAppCheckHTTPError *)error;
if ([HTTPError isKindOfClass:[GACAppCheckHTTPError class]] &&
HTTPError.HTTPResponse.statusCode == 403) {
GACLogDebug(kGACLoggerAppCheckMessageCodeAttestationRejected,
@"App Attest attestation was rejected by backend. The existing "
@"attestation will be reset.");
GACAppCheckLogDebug(GACLoggerAppCheckMessageCodeAttestationRejected,
@"App Attest attestation was rejected by backend. The existing "
@"attestation will be reset.");
// Reset the attestation.
return [self resetAttestation].thenOn(self.queue, ^NSError *(id result) {
// Throw the rejection error.
Expand Down
11 changes: 7 additions & 4 deletions AppCheck/Sources/Core/APIService/GACAppCheckAPIService.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@

#import "AppCheck/Sources/Core/APIService/GACAppCheckToken+APIResponse.h"
#import "AppCheck/Sources/Core/Errors/GACAppCheckErrorUtil.h"
#import "AppCheck/Sources/Core/GACAppCheckLogger.h"
#import "AppCheck/Sources/Core/GACAppCheckLogger+Internal.h"
#import "AppCheck/Sources/Public/AppCheck/GACAppCheckErrors.h"
#import "AppCheck/Sources/Public/AppCheck/GACAppCheckLogger.h"

#import <GoogleUtilities/GULURLSessionDataResponse.h>
#import <GoogleUtilities/NSURLSession+GULPromises.h>
Expand Down Expand Up @@ -129,9 +130,11 @@ - (instancetype)initWithURLSession:(NSURLSession *)session
NSInteger statusCode = response.HTTPResponse.statusCode;
return [FBLPromise do:^id _Nullable {
if (statusCode < 200 || statusCode >= 300) {
GACLogDebug(kGACLoggerAppCheckMessageCodeUnexpectedHTTPCode,
@"Unexpected API response: %@, body: %@.", response.HTTPResponse,
[[NSString alloc] initWithData:response.HTTPBody encoding:NSUTF8StringEncoding]);
NSString *logMessage = [NSString
stringWithFormat:@"Unexpected API response: %@, body: %@.", response.HTTPResponse,
[[NSString alloc] initWithData:response.HTTPBody
encoding:NSUTF8StringEncoding]];
GACAppCheckLogDebug(GACLoggerAppCheckMessageCodeUnexpectedHTTPCode, logMessage);
return [GACAppCheckErrorUtil APIErrorWithHTTPResponse:response.HTTPResponse
data:response.HTTPBody];
}
Expand Down
2 changes: 1 addition & 1 deletion AppCheck/Sources/Core/GACAppCheck.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#import "AppCheck/Sources/Public/AppCheck/GACAppCheckTokenDelegate.h"

#import "AppCheck/Sources/Core/Errors/GACAppCheckErrorUtil.h"
#import "AppCheck/Sources/Core/GACAppCheckLogger.h"
#import "AppCheck/Sources/Core/GACAppCheckLogger+Internal.h"
#import "AppCheck/Sources/Core/Storage/GACAppCheckStorage.h"
#import "AppCheck/Sources/Core/TokenRefresh/GACAppCheckTokenRefreshResult.h"
#import "AppCheck/Sources/Core/TokenRefresh/GACAppCheckTokenRefresher.h"
Expand Down
50 changes: 50 additions & 0 deletions AppCheck/Sources/Core/GACAppCheckLogger+Internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2020 Google LLC
*
* 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.
*/

#import <Foundation/Foundation.h>

#import "AppCheck/Sources/Public/AppCheck/GACAppCheckErrors.h"
#import "AppCheck/Sources/Public/AppCheck/GACAppCheckLogger.h"

NS_ASSUME_NONNULL_BEGIN

/** Prints the given code and message to the console.
*
* @param code The message code describing the nature of the log.
* @param logLevel The log level of this log.
* @param message The message string to log.
*/
FOUNDATION_EXPORT
void GACAppCheckLog(GACAppCheckMessageCode code,
GACAppCheckLogLevel logLevel,
NSString *_Nonnull message);

#define GACAppCheckLogFault(MESSAGE_CODE, MESSAGE) \
GACAppCheckLog(MESSAGE_CODE, GACAppCheckLogLevelFault, MESSAGE);

#define GACAppCheckLogError(MESSAGE_CODE, MESSAGE) \
GACAppCheckLog(MESSAGE_CODE, GACAppCheckLogLevelError, MESSAGE);

#define GACAppCheckLogWarning(MESSAGE_CODE, MESSAGE) \
GACAppCheckLog(MESSAGE_CODE, GACAppCheckLogLevelWarning, MESSAGE);

#define GACAppCheckLogInfo(MESSAGE_CODE, MESSAGE) \
GACAppCheckLog(MESSAGE_CODE, GACAppCheckLogLevelInfo, MESSAGE);

#define GACAppCheckLogDebug(MESSAGE_CODE, MESSAGE) \
GACAppCheckLog(MESSAGE_CODE, GACAppCheckLogLevelDebug, MESSAGE);

NS_ASSUME_NONNULL_END
32 changes: 0 additions & 32 deletions AppCheck/Sources/Core/GACAppCheckLogger.h

This file was deleted.

59 changes: 31 additions & 28 deletions AppCheck/Sources/Core/GACAppCheckLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,36 @@
* limitations under the License.
*/

#import "AppCheck/Sources/Core/GACAppCheckLogger.h"
#import "AppCheck/Sources/Public/AppCheck/GACAppCheckLogger.h"

NS_ASSUME_NONNULL_BEGIN
#import "AppCheck/Sources/Core/GACAppCheckLogger+Internal.h"

#pragma mark - Log Message Codes
NS_ASSUME_NONNULL_BEGIN

NSString *const kGACLoggerAppCheckMessageCodeUnknown = @"I-FAA001001";
#pragma mark - Public

// GACAppCheck.m
NSString *const kGACLoggerAppCheckMessageCodeProviderIsMissing = @"I-FAA002002";
volatile NSInteger gGACAppCheckLogLevel = GACAppCheckLogLevelError;

// GACAppCheckAPIService.m
NSString *const kGACLoggerAppCheckMessageCodeUnexpectedHTTPCode = @"I-FAA003001";
#pragma mark - Helpers

// GACAppCheckDebugProvider.m
NSString *const kGACLoggerAppCheckMessageDebugProviderFailedExchange = @"I-FAA004002";
NSString *GACAppCheckMessageCodeEnumToString(GACAppCheckMessageCode code) {
return [[NSString alloc] initWithFormat:@"I-GAC%06ld", (long)code];
}

// GACAppAttestProvider.m
NSString *const kGACLoggerAppCheckMessageCodeAppAttestNotSupported = @"I-FAA007001";
NSString *const kGACLoggerAppCheckMessageCodeAttestationRejected = @"I-FAA007002";
NSString *GACAppCheckLoggerLevelEnumToString(GACAppCheckLogLevel logLevel) {
switch (logLevel) {
case GACAppCheckLogLevelFault:
return @"Fault";
case GACAppCheckLogLevelError:
return @"Error";
case GACAppCheckLogLevelWarning:
return @"Warning";
case GACAppCheckLogLevelInfo:
return @"Info";
case GACAppCheckLogLevelDebug:
return @"Debug";
}
}

#pragma mark - Logging Functions

Expand All @@ -46,21 +56,14 @@
* yyyy-mm-dd hh:mm:ss.SSS sender[PID] <Debug> [GoogleSignIn/AppCheck][I-COR000002] Configure
* succeed.
*/
#define GAC_LOGGING_FUNCTION(level) \
void GACLog##level(NSString *messageCode, NSString *format, ...) { \
va_list args_ptr; \
va_start(args_ptr, format); \
NSString *message = [[NSString alloc] initWithFormat:format arguments:args_ptr]; \
va_end(args_ptr); \
NSLog(@"<" #level "> [AppCheck][%@] %@", messageCode, message); \
void GACAppCheckLog(GACAppCheckMessageCode code, GACAppCheckLogLevel logLevel, NSString *message) {
// Don't log anything in not debug builds.
#if !NDEBUG
if (logLevel >= gGACAppCheckLogLevel) {
NSLog(@"<%@> [AppCheckCore][%@] %@", GACAppCheckLoggerLevelEnumToString(logLevel),
GACAppCheckMessageCodeEnumToString(code), message);
}

GAC_LOGGING_FUNCTION(Error)
GAC_LOGGING_FUNCTION(Warning)
GAC_LOGGING_FUNCTION(Notice)
GAC_LOGGING_FUNCTION(Info)
GAC_LOGGING_FUNCTION(Debug)

#undef GAC_LOGGING_FUNCTION
#endif // !NDEBUG
}

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#import "AppCheck/Sources/Core/APIService/GACAppCheckToken+APIResponse.h"

#import "AppCheck/Sources/Core/Errors/GACAppCheckErrorUtil.h"
#import "AppCheck/Sources/Core/GACAppCheckLogger.h"
#import "AppCheck/Sources/Core/GACAppCheckLogger+Internal.h"

NS_ASSUME_NONNULL_BEGIN

Expand Down
7 changes: 4 additions & 3 deletions AppCheck/Sources/DebugProvider/GACAppCheckDebugProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#endif

#import "AppCheck/Sources/Core/APIService/GACAppCheckAPIService.h"
#import "AppCheck/Sources/Core/GACAppCheckLogger.h"
#import "AppCheck/Sources/Core/GACAppCheckLogger+Internal.h"
#import "AppCheck/Sources/DebugProvider/API/GACAppCheckDebugProviderAPIService.h"
#import "AppCheck/Sources/Public/AppCheck/GACAppCheckErrors.h"
#import "AppCheck/Sources/Public/AppCheck/GACAppCheckToken.h"
Expand Down Expand Up @@ -112,8 +112,9 @@ - (void)getTokenWithCompletion:(void (^)(GACAppCheckToken *_Nullable token,
return nil;
})
.catch(^void(NSError *error) {
GACLogDebug(kGACLoggerAppCheckMessageDebugProviderFailedExchange,
@"Failed to exchange debug token to app check token: %@", error);
NSString *logMessage = [NSString
stringWithFormat:@"Failed to exchange debug token to app check token: %@", error];
GACAppCheckLogDebug(GACLoggerAppCheckMessageDebugProviderFailedExchange, logMessage);
handler(nil, error);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#import "AppCheck/Sources/Core/APIService/GACAppCheckToken+APIResponse.h"

#import "AppCheck/Sources/Core/Errors/GACAppCheckErrorUtil.h"
#import "AppCheck/Sources/Core/GACAppCheckLogger.h"
#import "AppCheck/Sources/Core/GACAppCheckLogger+Internal.h"

NS_ASSUME_NONNULL_BEGIN

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#import "AppCheck/Sources/Core/APIService/GACAppCheckAPIService.h"
#import "AppCheck/Sources/Core/Backoff/GACAppCheckBackoffWrapper.h"
#import "AppCheck/Sources/Core/GACAppCheckLogger.h"
#import "AppCheck/Sources/Core/GACAppCheckLogger+Internal.h"
#import "AppCheck/Sources/DeviceCheckProvider/API/GACDeviceCheckAPIService.h"
#import "AppCheck/Sources/DeviceCheckProvider/DCDevice+GACDeviceCheckTokenGenerator.h"
#import "AppCheck/Sources/Public/AppCheck/GACAppCheckToken.h"
Expand Down
1 change: 1 addition & 0 deletions AppCheck/Sources/Public/AppCheck/AppCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#import "GACAppCheck.h"
#import "GACAppCheckErrors.h"
#import "GACAppCheckLogger.h"
#import "GACAppCheckProvider.h"
#import "GACAppCheckSettings.h"
#import "GACAppCheckToken.h"
Expand Down
19 changes: 11 additions & 8 deletions AppCheck/Sources/Public/AppCheck/GACAppCheckErrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ typedef NS_ERROR_ENUM(GACAppCheckErrorDomain, GACAppCheckErrorCode){

#pragma mark - Error Message Codes

FOUNDATION_EXPORT NSString *const kGACLoggerAppCheckMessageCodeUnknown;
typedef NS_ENUM(NSInteger, GACAppCheckMessageCode) {
GACLoggerAppCheckMessageCodeUnknown = 1001,

// App Check
FOUNDATION_EXPORT NSString *const kGACLoggerAppCheckMessageCodeUnexpectedHTTPCode;
// App Check
GACLoggerAppCheckMessageCodeProviderIsMissing = 2002,
GACLoggerAppCheckMessageCodeUnexpectedHTTPCode = 3001,

// Debug Provider
FOUNDATION_EXPORT NSString *const kGACLoggerAppCheckMessageDebugProviderFailedExchange;
// Debug Provider
GACLoggerAppCheckMessageDebugProviderFailedExchange = 4002,

// App Attest Provider
FOUNDATION_EXPORT NSString *const kGACLoggerAppCheckMessageCodeAppAttestNotSupported;
FOUNDATION_EXPORT NSString *const kGACLoggerAppCheckMessageCodeAttestationRejected;
// App Attest Provider
GACLoggerAppCheckMessageCodeAppAttestNotSupported = 7001,
GACLoggerAppCheckMessageCodeAttestationRejected = 7002
};
39 changes: 39 additions & 0 deletions AppCheck/Sources/Public/AppCheck/GACAppCheckLogger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2023 Google LLC
*
* 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.
*/

#import "GACAppCheckErrors.h"

/// The current logging level.
///
/// Messages with levels equal to or higher priority than `GACAppCheckLogLevel` will be printed,
/// where Fault > Error > Warning > Info > Debug.
///
/// Note: Declared as volatile to make getting and setting atomic.
FOUNDATION_EXPORT volatile NSInteger gGACAppCheckLogLevel;

/// Constants that specify the level of logging to perform in App Check Core.
typedef NS_ENUM(NSInteger, GACAppCheckLogLevel) {
/// The debug log level; equivalent to `OS_LOG_TYPE_DEBUG`.
GACAppCheckLogLevelDebug = 1,
/// The informational log level; equivalent to `OS_LOG_TYPE_INFO`.
GACAppCheckLogLevelInfo = 2,
/// The warning log level; equivalent to `OS_LOG_TYPE_DEFAULT`.
GACAppCheckLogLevelWarning = 3,
/// The error log level; equivalent to `OS_LOG_TYPE_ERROR`.
GACAppCheckLogLevelError = 4,
/// The fault log level; equivalent to `OS_LOG_TYPE_FAULT`.
GACAppCheckLogLevelFault = 5
} NS_SWIFT_NAME(AppCheckCoreLogLevel);

0 comments on commit 41c3bfd

Please sign in to comment.