Example #1
0
        public IOutput newInstance(string recordType)
        {
            if (_client == null)
            {
                throw new ProcessFatalException("The connection to MongoDB could not be established.");
            }


            lock (_schemas)
                if (!_schemas.ContainsKey(recordType))
                {
                    _schemas.Add(recordType, MongoDBOut.CreateInstance <GenericSchema>(_db, recordType, _outputConfig.ShardKeys[recordType]));

                    return(_schemas[recordType]);
                }
                else
                {
                    ISchema dest = _schemas[recordType];

                    if (!dest.VerifyOrCreateSchema())
                    {
                        _log.Warn($"Schema for {recordType} could not be verified or created, no data will be output for this record type.");
                        return(new Dummy());
                    }

                    return(dest);
                }
        }
Example #2
0
        public void write(IRecordRef which, IDictionary <string, object> record)
        {
            MongoDBOut outInst = _inst[which.RecordName];

            if (outInst == null)
            {
                throw new UnrecoverableOperationException($"Record reference for Mongo record {which.RecordName} is invalid.");
            }

            var descriptor = new IndexDescriptor()
            {
                Position = _cache.Position
            };

            var doc = outInst.BsonSerialize(record);

            using (var bw = new BsonBinaryWriter(_cache))
                BsonSerializer.Serialize <BsonDocument>(bw, doc);

            descriptor.RecordLength = _cache.Position - descriptor.Position;

            if (!_index.ContainsKey(which.RecordName))
            {
                _index.Add(which.RecordName, new LinkedList <IndexDescriptor>());
            }

            _index[which.RecordName].AddLast(descriptor);
            _recordCount++;
        }
Example #3
0
        static MongoDBOutFactory()
        {
            try
            {
                _outputConfig = Config.GetConfig <MongoOutConfig>(MongoOutConfig.SECTION_NAME);
                _conConfig    = Config.GetConfig <MongoConnectionConfig>(MongoConnectionConfig.SECTION_NAME);


                if (_log.IsDebugEnabled)
                {
                    foreach (var spec in _outputConfig.ShardKeys)
                    {
                        _log.DebugFormat("Shard Key: {0}", spec);
                    }
                }

                if (_outputConfig.ShardKeys.Count > 0)
                {
                    _log.InfoFormat("{0} calculated shard keys have been defined.", _outputConfig.ShardKeys.Count);
                }

                MongoUrl mu = GetMongoConnectionString();

                _client = new MongoClient(mu);

                if (!_client.ListDatabaseNames().ToList().Contains(mu.DatabaseName))
                {
                    _log.Warn($"Database {mu.DatabaseName} does not exist, it will be created.");
                }

                _db = _client.GetDatabase(mu.DatabaseName);


                // It is a violation of OOP principles for this component to know about these records.  At some point the schema
                // creation may be moved to an installer that initializes the DB prior to running the application.
                _schemas.Add(Config.Service.SASTScanDetailRecordName,
                             MongoDBOut.CreateInstance <SastDetailSchema>(_db, Config.Service.SASTScanDetailRecordName, _outputConfig.ShardKeys[Config.Service.SASTScanDetailRecordName]));

                _schemas.Add(Config.Service.SASTScanSummaryRecordName, MongoDBOut.CreateInstance <SastSummarySchema>(_db, Config.Service.SASTScanSummaryRecordName,
                                                                                                                     _outputConfig.ShardKeys[Config.Service.SASTScanSummaryRecordName]));

                _schemas.Add(Config.Service.SCAScanSummaryRecordName, MongoDBOut.CreateInstance <SCASummarySchema>(_db, Config.Service.SCAScanSummaryRecordName,
                                                                                                                   _outputConfig.ShardKeys[Config.Service.SCAScanSummaryRecordName]));

                _schemas.Add(Config.Service.SCAScanDetailRecordName, MongoDBOut.CreateInstance <SCADetailSchema>(_db, Config.Service.SCAScanDetailRecordName,
                                                                                                                 _outputConfig.ShardKeys[Config.Service.SCAScanDetailRecordName]));

                _schemas.Add(Config.Service.ProjectInfoRecordName, MongoDBOut.CreateInstance <ProjectInfoSchema>(_db, Config.Service.ProjectInfoRecordName,
                                                                                                                 _outputConfig.ShardKeys[Config.Service.ProjectInfoRecordName]));

                _schemas.Add(Config.Service.PolicyViolationsRecordName, MongoDBOut.CreateInstance <PolicyViolationsSchema>(_db, Config.Service.PolicyViolationsRecordName,
                                                                                                                           _outputConfig.ShardKeys[Config.Service.PolicyViolationsRecordName]));
            }
            catch (Exception ex)
            {
                _log.Error("Error initializing MongoDB connectivity.", ex);
                _client = null;
            }
        }
Example #4
0
        static MongoDBOutFactory()
        {
            try
            {
                _cfg = Config.GetConfig <MongoOutConfig>(MongoOutConfig.SECTION_NAME);

                foreach (var spec in _cfg.ShardKeys)
                {
                    _log.DebugFormat("Shard Key: {0}", spec);
                }

                if (_cfg.ShardKeys.Count > 0)
                {
                    _log.InfoFormat("{0} calculated shard keys have been defined.", _cfg.ShardKeys.Count);
                }

                var mu = new MongoUrl(_cfg.ConnectionString);
                _client = new MongoClient(mu);


                if (!_client.ListDatabaseNames().ToList().Contains(mu.DatabaseName))
                {
                    _log.Warn($"Database {mu.DatabaseName} does not exist, it will be created.");
                }

                _db = _client.GetDatabase(mu.DatabaseName);

                _schemas.Add(Config.Service.SASTScanDetailRecordName,
                             MongoDBOut.CreateInstance <SastDetailSchema>(_db, Config.Service.SASTScanDetailRecordName, _cfg.ShardKeys[Config.Service.SASTScanDetailRecordName]));

                _schemas.Add(Config.Service.SASTScanSummaryRecordName, MongoDBOut.CreateInstance <SastSummarySchema>(_db, Config.Service.SASTScanSummaryRecordName,
                                                                                                                     _cfg.ShardKeys[Config.Service.SASTScanSummaryRecordName]));

                _schemas.Add(Config.Service.SCAScanSummaryRecordName, MongoDBOut.CreateInstance <SCASummarySchema>(_db, Config.Service.SCAScanSummaryRecordName,
                                                                                                                   _cfg.ShardKeys[Config.Service.SCAScanSummaryRecordName]));

                _schemas.Add(Config.Service.SCAScanDetailRecordName, MongoDBOut.CreateInstance <SCADetailSchema>(_db, Config.Service.SCAScanDetailRecordName,
                                                                                                                 _cfg.ShardKeys[Config.Service.SCAScanDetailRecordName]));

                _schemas.Add(Config.Service.ProjectInfoRecordName, MongoDBOut.CreateInstance <ProjectInfoSchema>(_db, Config.Service.ProjectInfoRecordName,
                                                                                                                 _cfg.ShardKeys[Config.Service.ProjectInfoRecordName]));

                _schemas.Add(Config.Service.PolicyViolationsRecordName, MongoDBOut.CreateInstance <PolicyViolationsSchema>(_db, Config.Service.PolicyViolationsRecordName,
                                                                                                                           _cfg.ShardKeys[Config.Service.PolicyViolationsRecordName]));
            }
            catch (Exception ex)
            {
                _log.Error("Error initializing database connectivity.", ex);
            }
        }