def testRemoveDefaultServiceAccount(self): """Tests removing the default service account.""" credentials = service_account.Credentials(None, None, None) private_node_config = ndb_models.GetPrivateNodeConfig() private_node_config.default_credentials = credentials private_node_config.put() # Remove default auth self.app.delete( '/_ah/api/mtt/v1/private_node_config/default_service_account') # Verify that credentials were cleared private_node_config = ndb_models.GetPrivateNodeConfig() self.assertIsNone(private_node_config.default_credentials)
def UpgradeNdb(): """Update the host's ATS ndb.""" host_ndb_version = (ndb_models.GetPrivateNodeConfig().ndb_version or DEFAULT_NDB_VERSION) logging.info('Checking for ndb updates. Current ndb version: %s', host_ndb_version) while host_ndb_version in UPDATE_FUNCTION_MAP: # Find and apply update next_update = UPDATE_FUNCTION_MAP[host_ndb_version] next_update() host_ndb_version = ndb_models.GetPrivateNodeConfig().ndb_version logging.info('No more ndb updates. Current ndb version: %s', host_ndb_version)
def __init__(self, server_uuid, category, action, label=None, value=None, **kwargs): private_node_config = ndb_models.GetPrivateNodeConfig() # Required parameters self.v = _API_VERSION # API version self.tid = _TRACKING_ID # Tracking ID self.cid = server_uuid # Server ID self.t = _EVENT_TYPE # Event type self.ec = category # Event category self.ea = action # Event action # Optional parameters if label: self.el = label if value: self.ev = value # Custom dimensions and metrics setattr(self, _METRIC_KEYS['app_version'], env.VERSION) setattr(self, _METRIC_KEYS['is_google'], env.IS_GOOGLE) setattr(self, _METRIC_KEYS['user_tag'], private_node_config.gms_client_id) for key, value in six.iteritems(kwargs): if not _METRIC_KEYS[key]: logging.warning('Unknown metric key: %s', key) continue setattr(self, _METRIC_KEYS[key], value)
def testUpdate(self): data = { 'metrics_enabled': True, } self.app.put_json('/_ah/api/mtt/v1/private_node_config', data) self.assertEqual(self._CreatePrivateNodeConfig(data), ndb_models.GetPrivateNodeConfig())
def testUploadEvent_disabled(self, mock_urlopen): """Tests that events are not sent when metrics are disabled.""" private_node_config = ndb_models.GetPrivateNodeConfig() private_node_config.metrics_enabled = False private_node_config.put() uploaded = analytics_uploader._UploadEvent('category', 'action') self.assertFalse(uploaded) mock_urlopen.assert_not_called()
def _PrivateNodeConfigMessageConverter(msg): """Converts a PrivateNodeConfig message into an object.""" private_node_config = ndb_models.GetPrivateNodeConfig() private_node_config.ndb_version = msg.ndb_version private_node_config.metrics_enabled = msg.metrics_enabled private_node_config.gms_client_id = msg.gms_client_id private_node_config.setup_wizard_completed = msg.setup_wizard_completed return private_node_config
def Wrapper(): # Call original function logging.info('Updating ndb to version %s...', update_version) func() # Update version number private_node_config = ndb_models.GetPrivateNodeConfig() private_node_config.ndb_version = update_version private_node_config.put() logging.info('Ndb update %s completed.', update_version)
def Root(path): """Routes all other requests to index.html and angular.""" del path # unused analytics_tracking_id = '' if not env.IS_DEV_MODE and ndb_models.GetPrivateNodeConfig( ).metrics_enabled: analytics_tracking_id = 'G-XRN33KLKER' return flask.render_template('index.html', analytics_tracking_id=analytics_tracking_id, is_google=env.IS_GOOGLE)
def Root(_): """Routes all other requests to index.html and angular.""" private_node_config = ndb_models.GetPrivateNodeConfig() analytics_tracking_id = '' if not env.IS_DEV_MODE and private_node_config.metrics_enabled: analytics_tracking_id = 'UA-140187490-1' return flask.render_template('index.html', analytics_tracking_id=analytics_tracking_id, env=env, private_node_config=private_node_config)
def setUp(self): super(AnalyticsUploaderTest, self).setUp() env.VERSION = 'version' env.IS_GOOGLE = True analytics_uploader._UPLOAD_ERROR_COUNT.value = 0 analytics_uploader._TRACKING_ID = 'tracking_id' private_node_config = ndb_models.GetPrivateNodeConfig() private_node_config.server_uuid = 'server' private_node_config.metrics_enabled = True private_node_config.gms_client_id = 'test_user_tag' private_node_config.put()
def SetDefaultServiceAccount(self, request): """Sets the default service account key to use for all services. Body: Service account JSON key file data """ data = json.loads(request.value) credentials = service_account.Credentials.from_service_account_info( data) private_node_config = ndb_models.GetPrivateNodeConfig() private_node_config.default_credentials = credentials private_node_config.put() return message_types.VoidMessage()
def testSetDefaultServiceAccount(self, mock_parse_key): """Tests setting the default service account.""" # Mock parsing service account JSON key mock_parse_key.return_value = service_account.Credentials( None, None, None) # Set default auth self.app.put_json( '/_ah/api/mtt/v1/private_node_config/default_service_account', {'value': '{}'}) # Verify that credentials were set private_node_config = ndb_models.GetPrivateNodeConfig() self.assertIsNotNone(private_node_config.default_credentials)
def testInit_withDefaultCredentials(self): """Tests that build channels inherits the default auth credentials.""" credentials = authorized_user.Credentials(None) private_node_config = ndb_models.GetPrivateNodeConfig() private_node_config.default_credentials = credentials private_node_config.put() config = ndb_models.BuildChannelConfig(id='channel_id', provider_name='oauth2_provider') channel = build.BuildChannel(config) self.assertEqual(channel.id, 'channel_id') self.assertTrue(channel.is_valid) self.assertIsInstance(channel.provider, OAuth2BuildProvider) self.assertEqual(channel.auth_state, ndb_models.AuthorizationState.AUTHORIZED) self.assertIsNotNone(channel.provider.GetCredentials())
def _UploadEvent(category, action, **kwargs): """Uploads an event to GA if metrics are enabled.""" private_node_config = ndb_models.GetPrivateNodeConfig() if (env.IS_DEV_MODE or not private_node_config.metrics_enabled or _UPLOAD_ERROR_COUNT.value >= MAX_CONSECUTIVE_UPLOAD_ERRORS): logging.debug('Metrics disabled - skipping %s:%s', category, action) return False event = _Event(private_node_config.server_uuid, category, action, **kwargs) data = six.ensure_binary(urllib.parse.urlencode(dict(event))) request = urllib.request.Request(url=_GA_ENDPOINT, data=data, headers={'User-Agent': 'MTT'}) try: urllib.request.urlopen(request) _UPLOAD_ERROR_COUNT.value = 0 except: with _UPLOAD_ERROR_COUNT.get_lock(): _UPLOAD_ERROR_COUNT.value += 1 raise return True
def __init__(self, config): self.id = config.key.id() self.config = config # Find and instantiate provider provider_class = GetBuildProviderClass(config.provider_name) if not provider_class: self.auth_state = ndb_models.AuthorizationState.NOT_APPLICABLE return self._provider = provider_class() self._provider.UpdateOptions( **ndb_models.NameValuePair.ToDict(self.config.options)) # Load credentials and set authorization state self.auth_state = ndb_models.AuthorizationState.UNAUTHORIZED private_node_config = ndb_models.GetPrivateNodeConfig() if not self._provider.auth_methods: self.auth_state = ndb_models.AuthorizationState.NOT_APPLICABLE elif config.credentials or private_node_config.default_credentials: self.auth_state = ndb_models.AuthorizationState.AUTHORIZED self._provider.UpdateCredentials( config.credentials or private_node_config.default_credentials)
def Get(self, request): """Fetches the server's private node configuration.""" private_node_config = ndb_models.GetPrivateNodeConfig() return messages.Convert(private_node_config, messages.PrivateNodeConfig)
def RemoveDefaultServiceAccount(self, request): """Removes the default service account credentials.""" private_node_config = ndb_models.GetPrivateNodeConfig() private_node_config.default_credentials = None private_node_config.put() return message_types.VoidMessage()