/*
 * Copyright 2017 Google, Inc.
 *
 * 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.
 *
 */

package com.netflix.spinnaker.halyard.config.validate.v1.util;

import com.amazonaws.util.IOUtils;
import com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder;
import com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder;
import com.netflix.spinnaker.halyard.core.problem.v1.Problem;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ValidatingFileReader {
  public static String contents(ConfigProblemSetBuilder ps, String path) {
    try {
      return IOUtils.toString(new FileInputStream(path));
    } catch (FileNotFoundException e) {
      ps.addProblem(Problem.Severity.FATAL, "Cannot find provided path: " + e.getMessage() + ".");
      return null;
    } catch (IOException e) {
      ConfigProblemBuilder problemBuilder = ps.addProblem(Problem.Severity.FATAL, "Failed to read path \"" + path + "\": " + e.getMessage() + ".");
      if (e.getMessage().contains("Permission denied")) {
        problemBuilder.setRemediation("Halyard is running as user " + System.getProperty("user.name") + ". Make sure that user can read the requested file.");
      }
      return null;
    }
  }
}

@@ -1115,24 +1115,177 @@
 ing path) %7B%0A
+    String noAccessRemediation = %22Halyard is running as user %22 + System.getProperty(%22user.name%22) + %22. Make sure that user can read the requested file.%22;%0A
     try %7B%0A  
@@ -1385,16 +1385,54 @@
  %7B%0A     
+ ConfigProblemBuilder problemBuilder =
  ps.addP
@@ -1513,24 +1513,139 @@
 e() + %22.%22);%0A
+      if (e.getMessage().contains(%22denied%22)) %7B%0A        problemBuilder.setRemediation(noAccessRemediation);%0A      %7D%0A
       return
@@ -1868,19 +1868,8 @@
 ns(%22
-Permission 
 deni
@@ -1918,126 +1918,27 @@
 ion(
-%22Halyard is running as user %22 + System.getProperty(%22user.name%22) + %22. Make sure that user can read the requested file.%22
+noAccessRemediation
 );%0A 

Add no access remediation to FileNotFoundException (#337)