001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.server.config.rest;
018
019import org.apache.juneau.http.*;
020import org.apache.juneau.http.response.*;
021import org.apache.juneau.marshall.json.*;
022import org.apache.juneau.rest.server.*;
023import org.apache.juneau.rest.server.servlet.*;
024import org.apache.juneau.server.config.repository.*;
025
026/**
027 * REST resource for loading configuration from a Git repository.
028 *
029 * <p>
030 * Fetches configuration files for a given project and branch,
031 * combining application defaults with project-specific overrides.
032 */
033@Rest(path = "/configs/*", guards = ConfigApiKeyGuard.class)
034public class LoadConfigResource extends RestServlet {
035
036   private static final long serialVersionUID = 8247663789227304097L;
037
038   /**
039    * Returns configuration for the specified project and branch as JSON.
040    *
041    * @param project Project name.
042    * @param branch Branch name.
043    * @return JSON-serialized configuration map.
044    * @throws Exception If configuration cannot be loaded.
045    */
046   @RestGet(path = "/{project}/{branch}/*", produces = "application/json")
047   public String gets(@Path("project") String project, @Path("branch") String branch) throws Exception {
048      var jsonSerializer = JsonSerializer.DEFAULT_READABLE;
049
050      GetConfiguration config;
051      try {
052         config = new GetConfiguration(project, branch);
053      } catch (@SuppressWarnings("unused") IllegalArgumentException e) {
054         // Malformed project/branch value (empty, '..', backslash, NUL, or '/' in project).
055         throw new BadRequest("Invalid project or branch.");
056      }
057      try {
058         config.execute();
059      } catch (@SuppressWarnings("unused") IllegalArgumentException e) {
060         // A crafted project/branch that escapes the checkout root is rejected by
061         // FileUtils.resolveSafely(). Map that boundary violation to 403 with a generic
062         // message so the offending path is never echoed back to the client.
063         throw new Forbidden("Access denied.");
064      }
065
066      return jsonSerializer.write(config.get());
067   }
068}