Пример #1
0
 public ToolDetailViewModel(Workspace workspace) : base("Details", workspace)
 {
     ContentId   = Identifier;
     AnchorTitle = "Details";
     Content     = new FilePropertyProvider(workspace);
     IsVisible   = true;
 }
Пример #2
0
 public PropertyLister(Workspace workspace, Options options, CommandRepository commandRepository,
                       ExtendedExtendedPropertyProvider extendedPropertyProvider)
     : base(workspace, options, commandRepository)
 {
     _extendedPropertyProvider = extendedPropertyProvider;
     _contentProvider          = new FilePropertyProvider(workspace);
 }
        private static IBootstrapperSettings CreatePreProcessorSettings(IList <string> childArgs, IAnalysisPropertyProvider properties, IAnalysisPropertyProvider globalFileProperties, ILogger logger)
        {
            string hostUrl = TryGetHostUrl(properties, logger);

            if (hostUrl == null)
            {
                return(null); // URL is a required parameter in the pre-process phase
            }

            // If we're using the default properties file then we need to pass it
            // explicitly to the pre-processor (it's in a different folder and won't
            // be able to find it otherwise).
            FilePropertyProvider fileProvider = globalFileProperties as FilePropertyProvider;

            if (fileProvider != null && fileProvider.IsDefaultSettingsFile)
            {
                Debug.Assert(fileProvider.PropertiesFile != null);
                Debug.Assert(!string.IsNullOrEmpty(fileProvider.PropertiesFile.FilePath), "Expecting the properties file path to be set");
                childArgs.Add(string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}{1}", FilePropertyProvider.Prefix, fileProvider.PropertiesFile.FilePath));
            }

            IBootstrapperSettings settings = new BootstrapperSettings(
                AnalysisPhase.PreProcessing,
                childArgs,
                hostUrl,
                VerbosityCalculator.ComputeVerbosity(properties, logger),
                logger);

            return(settings);
        }
        private static void AssertExpectedPropertiesFile(string expectedFilePath, IAnalysisPropertyProvider actualProvider)
        {
            FilePropertyProvider fileProvider = AssertIsFilePropertyProvider(actualProvider);

            Assert.IsNotNull(fileProvider.PropertiesFile, "Properties file object should not be null");
            Assert.AreEqual(expectedFilePath, fileProvider.PropertiesFile.FilePath, "Properties were not loaded from the expected location");
        }
        private static void CheckProcessingFails(IEnumerable <ArgumentInstance> cmdLineArgs, string defaultPropertiesDirectory, TestLogger logger)
        {
            var isValid = FilePropertyProvider.TryCreateProvider(cmdLineArgs, defaultPropertiesDirectory, logger, out var provider);

            isValid.Should().BeFalse("Not expecting the provider to be initialized successfully");
            provider.Should().BeNull("Not expecting a provider instance if the function returned true");
            logger.AssertErrorsLogged();
        }
        private static void CheckProcessingFails(IEnumerable <ArgumentInstance> cmdLineArgs, string defaultPropertiesDirectory, TestLogger logger)
        {
            bool isValid = FilePropertyProvider.TryCreateProvider(cmdLineArgs, defaultPropertiesDirectory, logger, out IAnalysisPropertyProvider provider);

            Assert.IsFalse(isValid, "Not expecting the provider to be initialized successfully");
            Assert.IsNull(provider, "Not expecting a provider instance if the function returned true");
            logger.AssertErrorsLogged();
        }
Пример #7
0
        /// <summary>
        /// Attempts to process the supplied command line arguments and
        /// reports any errors using the logger.
        /// Returns null unless all of the properties are valid.
        /// </summary>
        public static ProcessedArgs TryProcessArgs(string[] commandLineArgs, ILogger logger)
        {
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            ProcessedArgs processed = null;
            IEnumerable <ArgumentInstance> arguments;

            // This call will fail if there are duplicate, missing, or unrecognized arguments
            CommandLineParser parser = new CommandLineParser(Descriptors, false /* don't allow unrecognized */);
            bool parsedOk            = parser.ParseArguments(commandLineArgs, logger, out arguments);

            // Handle the /install: command line only argument
            bool installLoaderTargets;

            parsedOk &= TryGetInstallTargetsEnabled(arguments, logger, out installLoaderTargets);

            // Handler for command line analysis properties
            IAnalysisPropertyProvider cmdLineProperties;

            parsedOk &= CmdLineArgPropertyProvider.TryCreateProvider(arguments, logger, out cmdLineProperties);

            // Handler for scanner environment properties
            IAnalysisPropertyProvider scannerEnvProperties;

            parsedOk &= EnvScannerPropertiesProvider.TryCreateProvider(logger, out scannerEnvProperties);

            // Handler for property file
            IAnalysisPropertyProvider globalFileProperties;
            string asmPath = Path.GetDirectoryName(typeof(PreProcessor.ArgumentProcessor).Assembly.Location);

            parsedOk &= FilePropertyProvider.TryCreateProvider(arguments, asmPath, logger, out globalFileProperties);

            if (parsedOk)
            {
                Debug.Assert(cmdLineProperties != null);
                Debug.Assert(globalFileProperties != null);

                processed = new ProcessedArgs(
                    GetArgumentValue(KeywordIds.ProjectKey, arguments),
                    GetArgumentValue(KeywordIds.ProjectName, arguments),
                    GetArgumentValue(KeywordIds.ProjectVersion, arguments),
                    GetArgumentValue(KeywordIds.Organization, arguments),
                    installLoaderTargets,
                    cmdLineProperties,
                    globalFileProperties,
                    scannerEnvProperties);

                if (!AreParsedArgumentsValid(processed, logger))
                {
                    processed = null;
                }
            }

            return(processed);
        }
        /// <summary>
        /// Attempts to process the supplied command line arguments and reports any errors using the logger.
        /// Returns false if any parsing errors were encountered.
        /// </summary>
        public static bool TryProcessArgs(string[] commandLineArgs, ILogger logger, out IBootstrapperSettings settings)
        {
            if (commandLineArgs == null)
            {
                throw new ArgumentNullException("commandLineArgs");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            settings = null;

            IEnumerable <ArgumentInstance> arguments;

            // This call will fail if there are duplicate or missing arguments
            CommandLineParser parser = new CommandLineParser(Descriptors, true /* allow unrecognized arguments*/);
            bool parsedOk            = parser.ParseArguments(commandLineArgs, logger, out arguments);

            // Handler for command line analysis properties
            IAnalysisPropertyProvider cmdLineProperties;

            parsedOk &= CmdLineArgPropertyProvider.TryCreateProvider(arguments, logger, out cmdLineProperties);

            // Handler for property file
            IAnalysisPropertyProvider globalFileProperties;
            string asmPath = Path.GetDirectoryName(typeof(Bootstrapper.ArgumentProcessor).Assembly.Location);

            parsedOk &= FilePropertyProvider.TryCreateProvider(arguments, asmPath, logger, out globalFileProperties);

            AnalysisPhase phase;

            parsedOk &= TryGetPhase(commandLineArgs.Length, arguments, logger, out phase);

            Debug.Assert(!parsedOk || cmdLineProperties != null);
            Debug.Assert(!parsedOk || globalFileProperties != null);

            if (parsedOk)
            {
                Debug.Assert(cmdLineProperties != null);
                Debug.Assert(globalFileProperties != null);
                IAnalysisPropertyProvider properties = new AggregatePropertiesProvider(cmdLineProperties, globalFileProperties);

                IList <string> baseChildArgs = RemoveBootstrapperArgs(commandLineArgs);

                if (phase == AnalysisPhase.PreProcessing)
                {
                    settings = CreatePreProcessorSettings(baseChildArgs, properties, globalFileProperties, logger);
                }
                else
                {
                    settings = CreatePostProcessorSettings(baseChildArgs, properties, logger);
                }
            }

            return(settings != null);
        }
        private static IAnalysisPropertyProvider CheckProcessingSucceeds(IEnumerable <ArgumentInstance> cmdLineArgs, string defaultPropertiesDirectory, TestLogger logger)
        {
            var isValid = FilePropertyProvider.TryCreateProvider(cmdLineArgs, defaultPropertiesDirectory, logger, out var provider);

            isValid.Should().BeTrue("Expecting the provider to be initialized successfully");
            provider.Should().NotBeNull("Not expecting a null provider if the function returned true");
            logger.AssertErrorsLogged(0);

            return(provider);
        }
        public void AnalysisConfGen_FileProperties()
        {
            // File properties should not be copied to the file.
            // Instead, a pointer to the file should be created.

            // Arrange
            var analysisDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext);

            var logger = new TestLogger();

            // The set of file properties to supply
            var fileProperties = new AnalysisProperties
            {
                new Property()
                {
                    Id = SonarProperties.HostUrl, Value = "http://myserver"
                },
                new Property()
                {
                    Id = "file.only", Value = "file value"
                }
            };
            var settingsFilePath = Path.Combine(analysisDir, "settings.txt");

            fileProperties.Save(settingsFilePath);

            var fileProvider = FilePropertyProvider.Load(settingsFilePath);

            var args = new ProcessedArgs("key", "name", "version", "organization", false, EmptyPropertyProvider.Instance, fileProvider, EmptyPropertyProvider.Instance, logger);

            var settings = TeamBuildSettings.CreateNonTeamBuildSettingsForTesting(analysisDir);

            Directory.CreateDirectory(settings.SonarConfigDirectory); // config directory needs to exist

            // Act
            var actualConfig = AnalysisConfigGenerator.GenerateFile(args, settings, new Dictionary <string, string>(), new List <AnalyzerSettings>(), new MockSonarQubeServer(), logger);

            // Assert
            AssertConfigFileExists(actualConfig);
            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);

            var actualSettingsFilePath = actualConfig.GetSettingsFilePath();

            actualSettingsFilePath.Should().Be(settingsFilePath, "Unexpected settings file path");

            // Check the file setting value do not appear in the config file
            AssertFileDoesNotContainText(actualConfig.FileName, "file.only");

            actualConfig.SourcesDirectory.Should().Be(settings.SourcesDirectory);
            actualConfig.SonarScannerWorkingDirectory.Should().Be(settings.SonarScannerWorkingDirectory);
            AssertExpectedLocalSetting(SonarProperties.Organization, "organization", actualConfig);
        }
        public void FileProvider_InvalidArguments()
        {
            // 0. Setup
            IAnalysisPropertyProvider provider;

            // 1. Null command line arguments
            AssertException.Expects <ArgumentNullException>(() => FilePropertyProvider.TryCreateProvider(null, string.Empty, new TestLogger(), out provider));

            // 2. Null directory
            AssertException.Expects <ArgumentNullException>(() => FilePropertyProvider.TryCreateProvider(Enumerable.Empty <ArgumentInstance>(), null, new TestLogger(), out provider));

            // 3. Null logger
            AssertException.Expects <ArgumentNullException>(() => FilePropertyProvider.TryCreateProvider(Enumerable.Empty <ArgumentInstance>(), string.Empty, null, out provider));
        }
        public void AnalysisConfGen_FileProperties()
        {
            // File properties should not be copied to the file.
            // Instead, a pointer to the file should be created.

            // Arrange
            string analysisDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            TestLogger logger = new TestLogger();

            // The set of file properties to supply
            AnalysisProperties fileProperties = new AnalysisProperties();

            fileProperties.Add(new Property()
            {
                Id = SonarProperties.HostUrl, Value = "http://myserver"
            });
            fileProperties.Add(new Property()
            {
                Id = "file.only", Value = "file value"
            });
            string settingsFilePath = Path.Combine(analysisDir, "settings.txt");

            fileProperties.Save(settingsFilePath);

            FilePropertyProvider fileProvider = FilePropertyProvider.Load(settingsFilePath);

            ProcessedArgs args = new ProcessedArgs("key", "name", "version", false, EmptyPropertyProvider.Instance, fileProvider);

            TeamBuildSettings settings = TeamBuildSettings.CreateNonTeamBuildSettingsForTesting(analysisDir);

            Directory.CreateDirectory(settings.SonarConfigDirectory); // config directory needs to exist

            // Act
            AnalysisConfig actualConfig = AnalysisConfigGenerator.GenerateFile(args, settings, new Dictionary <string, string>(), logger);

            // Assert
            AssertConfigFileExists(actualConfig);
            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);

            string actualSettingsFilePath = actualConfig.GetSettingsFilePath();

            Assert.AreEqual(settingsFilePath, actualSettingsFilePath, "Unexpected settings file path");

            // Check the file setting value do not appear in the config file
            AssertFileDoesNotContainText(actualConfig.FileName, "file.only");

            Assert.AreEqual(settings.SonarRunnerWorkingDirectory, actualConfig.SonarRunnerWorkingDirectory);
        }
Пример #13
0
        private static IBootstrapperSettings CreatePreProcessorSettings(ICollection <string> childArgs, IAnalysisPropertyProvider properties, IAnalysisPropertyProvider globalFileProperties, ILogger logger)
        {
            // If we're using the default properties file then we need to pass it
            // explicitly to the pre-processor (it's in a different folder and won't
            // be able to find it otherwise).
            FilePropertyProvider fileProvider = globalFileProperties as FilePropertyProvider;

            if (fileProvider != null && fileProvider.IsDefaultSettingsFile)
            {
                Debug.Assert(fileProvider.PropertiesFile != null);
                Debug.Assert(!string.IsNullOrEmpty(fileProvider.PropertiesFile.FilePath), "Expecting the properties file path to be set");
                childArgs.Add(string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}{1}", FilePropertyProvider.Prefix, fileProvider.PropertiesFile.FilePath));
            }

            return(CreateSettings(AnalysisPhase.PreProcessing, childArgs, properties, logger));
        }
        public void FileProvider_InvalidArguments()
        {
            // 0. Setup
            IAnalysisPropertyProvider provider;

            // 1. Null command line arguments
            Action act = () => FilePropertyProvider.TryCreateProvider(null, string.Empty, new TestLogger(), out provider);

            act.Should().ThrowExactly <ArgumentNullException>();

            // 2. Null directory
            act = () => FilePropertyProvider.TryCreateProvider(Enumerable.Empty <ArgumentInstance>(), null, new TestLogger(), out provider);
            act.Should().ThrowExactly <ArgumentNullException>();

            // 3. Null logger
            act = () => FilePropertyProvider.TryCreateProvider(Enumerable.Empty <ArgumentInstance>(), string.Empty, null, out provider);
            act.Should().ThrowExactly <ArgumentNullException>();
        }
        private static void AssertIsNotDefaultPropertiesFile(IAnalysisPropertyProvider actualProvider)
        {
            FilePropertyProvider fileProvider = AssertIsFilePropertyProvider(actualProvider);

            Assert.IsFalse(fileProvider.IsDefaultSettingsFile, "Not expecting the provider to be marked as using the default properties file");
        }
        [WorkItem(127)] // Do not store the db and server credentials in the config files: http://jira.sonarsource.com/browse/SONARMSBRU-127
        public void AnalysisConfGen_AnalysisConfigDoesNotContainSensitiveData()
        {
            // Arrange
            var analysisDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext);

            var logger = new TestLogger();

            var cmdLineArgs = new ListPropertiesProvider();

            // Public args - should be written to the config file
            cmdLineArgs.AddProperty("sonar.host.url", "http://host");
            cmdLineArgs.AddProperty("public.key", "public value");
            cmdLineArgs.AddProperty("sonar.user.license.secured", "user input license");
            cmdLineArgs.AddProperty("server.key.secured.xxx", "not really secure");
            cmdLineArgs.AddProperty("sonar.value", "value.secured");

            // Sensitive values - should not be written to the config file
            cmdLineArgs.AddProperty(SonarProperties.DbPassword, "secret db password");

            // Create a settings file with public and sensitive data
            var fileSettings = new AnalysisProperties
            {
                new Property()
                {
                    Id = "file.public.key", Value = "file public value"
                },
                new Property()
                {
                    Id = SonarProperties.DbUserName, Value = "secret db user"
                },
                new Property()
                {
                    Id = SonarProperties.DbPassword, Value = "secret db password"
                }
            };
            var fileSettingsPath = Path.Combine(analysisDir, "fileSettings.txt");

            fileSettings.Save(fileSettingsPath);
            var fileProvider = FilePropertyProvider.Load(fileSettingsPath);

            var args = new ProcessedArgs("key", "name", "1.0", null, false, cmdLineArgs, fileProvider, EmptyPropertyProvider.Instance, logger);

            IDictionary <string, string> serverProperties = new Dictionary <string, string>
            {
                // Public server settings
                { "server.key.1", "server value 1" },
                // Sensitive server settings
                { SonarProperties.SonarUserName, "secret user" },
                { SonarProperties.SonarPassword, "secret pwd" },
                { "sonar.vbnet.license.secured", "secret license" },
                { "sonar.cpp.License.Secured", "secret license 2" }
            };

            var settings = TeamBuildSettings.CreateNonTeamBuildSettingsForTesting(analysisDir);

            Directory.CreateDirectory(settings.SonarConfigDirectory); // config directory needs to exist

            // Act
            var config = AnalysisConfigGenerator.GenerateFile(args, settings, serverProperties, new List <AnalyzerSettings>(), new MockSonarQubeServer(), logger);

            // Assert
            AssertConfigFileExists(config);
            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);

            // Check the config

            // "Public" arguments should be in the file
            config.SonarProjectKey.Should().Be("key", "Unexpected project key");
            config.SonarProjectName.Should().Be("name", "Unexpected project name");
            config.SonarProjectVersion.Should().Be("1.0", "Unexpected project version");

            AssertExpectedLocalSetting(SonarProperties.HostUrl, "http://host", config);
            AssertExpectedLocalSetting("sonar.user.license.secured", "user input license", config); // we only filter out *.secured server settings
            AssertExpectedLocalSetting("sonar.value", "value.secured", config);
            AssertExpectedLocalSetting("server.key.secured.xxx", "not really secure", config);
            AssertExpectedServerSetting("server.key.1", "server value 1", config);

            AssertFileDoesNotContainText(config.FileName, "file.public.key"); // file settings values should not be in the config
            AssertFileDoesNotContainText(config.FileName, "secret");          // sensitive data should not be in config
        }
        [WorkItem(127)] // Do not store the db and server credentials in the config files: http://jira.sonarsource.com/browse/SONARMSBRU-127
        public void AnalysisConfGen_AnalysisConfigDoesNotContainSensitiveData()
        {
            // Arrange
            string analysisDir = TestUtils.CreateTestSpecificFolder(this.TestContext);

            TestLogger logger = new TestLogger();

            ListPropertiesProvider cmdLineArgs = new ListPropertiesProvider();

            // Public args - should be written to the config file
            cmdLineArgs.AddProperty("sonar.host.url", "http://host");
            cmdLineArgs.AddProperty("public.key", "public value");

            // Sensitive values - should not be written to the config file
            cmdLineArgs.AddProperty(SonarProperties.DbPassword, "secret db password");
            cmdLineArgs.AddProperty(SonarProperties.DbUserName, "secret db user");

            // Create a settings file with public and sensitive data
            AnalysisProperties fileSettings = new AnalysisProperties();

            fileSettings.Add(new Property()
            {
                Id = "file.public.key", Value = "file public value"
            });
            fileSettings.Add(new Property()
            {
                Id = SonarProperties.DbUserName, Value = "secret db user"
            });
            fileSettings.Add(new Property()
            {
                Id = SonarProperties.DbPassword, Value = "secret db password"
            });
            string fileSettingsPath = Path.Combine(analysisDir, "fileSettings.txt");

            fileSettings.Save(fileSettingsPath);
            FilePropertyProvider fileProvider = FilePropertyProvider.Load(fileSettingsPath);

            ProcessedArgs args = new ProcessedArgs("key", "name", "1.0", false, cmdLineArgs, fileProvider);

            IDictionary <string, string> serverProperties = new Dictionary <string, string>();

            // Public server settings
            serverProperties.Add("server.key.1", "server value 1");
            // Sensitive server settings
            serverProperties.Add(SonarProperties.SonarUserName, "secret user");
            serverProperties.Add(SonarProperties.SonarPassword, "secret pwd");

            TeamBuildSettings settings = TeamBuildSettings.CreateNonTeamBuildSettingsForTesting(analysisDir);

            Directory.CreateDirectory(settings.SonarConfigDirectory); // config directory needs to exist

            // Act
            AnalysisConfig config = AnalysisConfigGenerator.GenerateFile(args, settings, serverProperties, logger);

            // Assert
            AssertConfigFileExists(config);
            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);

            // Check the config

            // "Public" arguments should be in the file
            Assert.AreEqual("key", config.SonarProjectKey, "Unexpected project key");
            Assert.AreEqual("name", config.SonarProjectName, "Unexpected project name");
            Assert.AreEqual("1.0", config.SonarProjectVersion, "Unexpected project version");

            AssertExpectedLocalSetting(SonarProperties.HostUrl, "http://host", config);
            AssertExpectedServerSetting("server.key.1", "server value 1", config);

            AssertFileDoesNotContainText(config.FileName, "file.public.key"); // file settings values should not be in the config

            // SONARMSBRU-136: TODO - uncomment the following code:
            AssertFileDoesNotContainText(config.FileName, "secret"); // sensitive data should not be in config
        }