'분류 전체보기'에 해당되는 글 1583건

  1. 2020.08.14 [Ansible] apt lock 문제.
  2. 2020.08.04 [JSON] JSON.parse(), JSON.stringify()
  3. 2020.07.31 [Terraform] S3 Backend 사용.
  4. 2020.07.23 [Spring] Spring Security Session Timeout Disable.
  5. 2020.07.21 [Java] jar 파일 내 resources 읽기 - ClassPathResource
  6. 2020.07.21 [Spring] viewResolver return types...
  7. 2020.07.21 [Terraform] apply 후 생성 된 정보 구하기.
  8. 2020.07.20 [AWS] Java SDK 2.x 사용 - build.gradle
  9. 2020.07.13 [AWS] DescribeInstanceTypes from Java
  10. 2020.07.07 [Spring] Spring Security 기본 Login Form 제거 및 CSRF 만 사용하기

[Ansible] apt lock 문제.

Cloud&Container/IaC 2020. 8. 14. 14:27

ansible 을 이용해서 서버에 필요한 stack 들을 설치 해야 하는 경우 아래와 같은 에러가 발생 할 때가 있습니다.

 

[에러]

...중략...

E: Could not get lock /var/lib/dpkg/lock

...중략...

정리 하면 다른 프로세스가 이미 사용중이기 때문에 사용할 수 없다는 뭐 그런 이야기 입니다.

그렇다 보니 ansible 배포가 실패 하게 되는데요.

 

이를 해결 하기 위해서 wait_for 나 process check 나 retry 등 다양하게 해봤는데, 그나마 아래 코드가 제일 잘 동작해서 올려 봅니다.

 

[해결]

- name: Apt lock check
  apt: name=build-essential state=present
  register: apt_state
  until: apt_state is success
  delay: 30
  retries: 10

 

:

[JSON] JSON.parse(), JSON.stringify()

ITWeb/개발일반 2020. 8. 4. 16:49

[참고문서]

https://www.w3schools.com/js/js_json_parse.asp

https://www.w3schools.com/js/js_json_stringify.asp

 

String to JSON Object)

JSON_OBJECT = JSON.parse("JSON String");

 

JSON Object to String)

JSON_STRING = JSON.stringify(JSON_OBJECT);

 

:

[Terraform] S3 Backend 사용.

Cloud&Container/IaC 2020. 7. 31. 14:21

공식 문서는 아래 참고 하세요.

https://www.terraform.io/docs/backends/types/s3.html

 

terraform {
  backend "s3" {
    bucket = "버킷이름"
    key    = "폴더/파일"
    region = "ap-northeast-2"
  }
}
  • s3 backend 를 사용하기 위해서 먼저 s3 bucket 을 생성 합니다.
  • key 부분은 미리 생성해 놓지 않아도 terraform 실행을 하시면 생성이 됩니다.
  • 만약 bucket 을 만들어 놓지 않고 실행을 하게 되면 아래와 같은 에러가 발생 합니다.
Error refreshing state: BucketRegionError: incorrect region, 
the bucket is not in 'ap-northeast-2' region at endpoint ''
	status code: 301, request id: , host id:
  • bucket 을 생성 하고  $ terraform init 을 해도 같은 에러가 계속 발생 합니다.
  • 이 경우 .terraform 폴더를 삭제 하고 다시 시도 하시면 정상적으로 동작 합니다.

Terraform 기본 명령어)

$ terraform init

$ terraform plan

$ terraform apply

$ terraform destroy

.aws 에 aws configure 를 통한 설정이 되어 있어야 합니다.

 

 

:

[Spring] Spring Security Session Timeout Disable.

ITWeb/개발일반 2020. 7. 23. 07:55

application.yml 또는 properties 파일 내 아래와 같이 선언 하시면 됩니다.

server:
  servlet:
    session:
      timeout: 0

 

:

[Java] jar 파일 내 resources 읽기 - ClassPathResource

ITWeb/개발일반 2020. 7. 21. 20:23

제목에 있는 그대로 입니다.

 

[참고문서]

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/ClassPathResource.html

 

ClassPathResource 를 이용해서 구현 하면 됩니다.

// resourceFile 은 resources 를 제외한 path 와 filename 을 작성 합니다.

ClassPathResource resource = new ClassPathResource(resourceFile);

 

아래 예제 코드는 programcreek.com 에서 가져 왔습니다.

public static String getResourceAsString(String path) {
	try {
		Resource resource = new ClassPathResource(path);
		try (BufferedReader br = new BufferedReader(
				new InputStreamReader(resource.getInputStream()))) {
			StringBuilder builder = new StringBuilder();
			String line;
			while ((line = br.readLine()) != null) {
				builder.append(line).append('\n');
			}
			return builder.toString();
		}
	}
	catch (IOException e) {
		throw new IllegalStateException(e);
	}
}

이와 같이 구현을 한 이유는 로컬에서는 잘 되는데 jar 로 말았을 때는 file 을 찾지 못하는 에러가 발생을 해서 입니다.

:

[Spring] viewResolver return types...

ITWeb/개발일반 2020. 7. 21. 10:45

[참고문서]

https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/htmlsingle/#mvc-ann-return-types

https://docs.spring.io/spring/docs/3.0.0.M3/spring-framework-reference/html/ch16s03.html

 

Spring mvc framework 에서 Controller 에서 사용하는 return type 에 따른 viewResolver 적용방법이 다릅니다.

위 문서를 보시면 이해 하실 수 있습니다.

 

가장 많이 사용 하는 몇 개만 뽑아 왔습니다.

 

  • A ModelAndView object, with the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.
  • A Model object, with the view name implicitly determined through a RequestToViewNameTranslator and the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.
  • A Map object for exposing a model, with the view name implicitly determined through a RequestToViewNameTranslator and the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.
  • A View object, with the model implicitly determined through command objects and @ModelAttribute annotated reference data accessor methods. The handler method may also programmatically enrich the model by declaring a Model argument (see above).
  • A String value that is interpreted as the logical view name, with the model implicitly determined through command objects and @ModelAttribute annotated reference data accessor methods. The handler method may also programmatically enrich the model by declaring a Model argument (see above).
  • void if the method handles the response itself (by writing the response content directly, declaring an argument of type ServletResponse / HttpServletResponse for that purpose) or if the view name is supposed to be implicitly determined through a RequestToViewNameTranslator (not declaring a response argument in the handler method signature).
  • If the method is annotated with @ResponseBody, the return type is written to the response HTTP body. The return value will be converted to the declared method argument type using HttpMessageConverters. See the section called “Mapping the response body with the @ResponseBody annotation”.

제가 실수한 부분은 void 로 선언을 해 놓고 template 수정 후 반영이 되지 않아 cache 를 의심 했었는데 역시 원인은 제가 선언을 잘 못 했기 때문 이였습니다.

 

void 로 선언 시 해석은 

@GetMapping("/hello")
public void helloworld() {...}

hello.html 을 template 으로 찾게 됩니다.

 

:

[Terraform] apply 후 생성 된 정보 구하기.

ITWeb/개발일반 2020. 7. 21. 08:44

Terraform  을 이용해서 인프라 구성을 한 후에 생성된 정보를 얻어 와야 할 때가 있습니다.

특정 정보만 구하고 싶을 경우 output 설정을 이용해서 얻을 수 있는 방법과 state 를 이용해서 얻을 수 있는 방법이 있습니다.

 

[참고문서]

https://www.terraform.io/docs/commands/output.html

 

tf 설정 파일 내 output 설정을 합니다.

resource "aws_instance" "allinone" {
...중략...
  count = 3
}

output "private_ip" {
  value = "${aws_instance.allinone.*.private_ip}"
}
# 변수 정보를 정상적으로 호출 하지 못할 경우 실행
$ terraform refresh

$ terraform output 변수명
-->
$ terraform output private_ip
[
  "10.0.25.14",
]

 

아래와 같이 하면 모든 state 정보를 return 해 줍니다.

 

$ terraform state pull

 

:

[AWS] Java SDK 2.x 사용 - build.gradle

Cloud&Container/AWS 2020. 7. 20. 08:58

1.x 와는 많이 바뀌었습니다.

확인 하고 변경 하세요.

 

implementation platform('software.amazon.awssdk:bom:2.13.55')
// aws sdk
compile 'software.amazon.awssdk:sdk-core'
compile 'software.amazon.awssdk:ec2'
compile 'software.amazon.awssdk:s3'
compile 'software.amazon.awssdk:sqs'

:

[AWS] DescribeInstanceTypes from Java

Cloud&Container/AWS 2020. 7. 13. 18:06

본 API 가 삭제 되었기 때문에 그냥 아래와 같이 단순하게 구성해 보았습니다.

 

@Service
@Log4j2
@RequiredArgsConstructor
public class Ec2Service {

  public ResponseEntity<String> describeInstanceTypes() {
    ProcessBuilder builder = new ProcessBuilder();
    String instances = "{}";

    try {
      builder.command("bash", "-c", "aws ec2 describe-instance-types");
      Process process = builder.start();

      instances = new BufferedReader(
        new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)).lines()
        .collect(Collectors.joining("\n"));

      int exitCode = process.waitFor();

      JSONObject jsonObject = new JSONObject(instances);
      JSONArray jsonArray = jsonObject.getJSONArray("InstanceTypes");
      JSONArray sortedJsonArray = new JSONArray();
      List list = new ArrayList();

      for(int i = 0; i < jsonArray.length(); i++) {
        list.add(jsonArray.getJSONObject(i));
      }

      Collections.sort(list, new Comparator<JSONObject>() {

        @Override
        public int compare(JSONObject a, JSONObject b) {
          String source = new String();
          String target = new String();

          try {
            source = (String)a.get("InstanceType");
            target = (String)b.get("InstanceType");
          } catch(Exception e) {
          }

          return source.compareTo(target);
        }
      });

      for(int i = 0; i < jsonArray.length(); i++) {
        sortedJsonArray.put(list.get(i));
      }

      instances = "{\"InstanceTypes\": " +sortedJsonArray.toString()+ "}";
    } catch(Exception e) {
      e.printStackTrace();
    }

    return ResponseEntity.ok(instances);
  }
}

 

코드 자체는 AWS CLI 를 ProcessBuilder 를 이용해서 외부 파일을 실행 시켜 데이터를 받아와서 처리 하는 내용입니다.

AWS CLI 로 데이터를 가져 오게 되면 정렬이 안된 상태로 데이터가 넘어 와서 별도 sort 기능 구현이 필요 합니다.

그냥 CLI 에서도 JSON Sort 옵션 하나 넣어 주면 좋았을 걸 아쉽더라고요.

 

근데 왜 이 API 를 없앴는지 차암....

 

1.x 에서 삭제 되어서 2.x 로 변경해서 사용 하시면 됩니다.

    Ec2Client ec2Client = Ec2Client.builder().build();
    DescribeInstanceTypesRequest request = DescribeInstanceTypesRequest.builder()
      .maxResults(100)
      .build();

    DescribeInstanceTypesResponse response = ec2Client.describeInstanceTypes(request);
    log.debug("{}", response.instanceTypes());

한번에 100개 씩 밖에 못 가져 오기 때문에 nextToken 으로 끝까지 요청 하셔야 합니다.

:

[Spring] Spring Security 기본 Login Form 제거 및 CSRF 만 사용하기

ITWeb/개발일반 2020. 7. 7. 08:33

사용하다 보면 별의 별 요구사항이 나오게 됩니다.

단순 하게 CSRF 만 사용 하고 싶은데 자꾸 login form 이 나와서 설정만으로 이걸 해결해 보고자 했습니다.

그러나 설정 만으로는 안되더라고요.

 

설정 예시) 비추천

security:
  enable:
    csrf: true
  basic:
    enabled: false

management:
  security:
    enabled: false

 

코드 예시) 추천

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity.httpBasic().disable();
  }
}

참고 정보)

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-quartz'
  implementation 'org.springframework.boot:spring-boot-starter-web'

  // thymeleaf
  implementation 'org.thymeleaf.extras:thymeleaf-extras-java8time'
  implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
  implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'

  // spring security
  implementation 'org.springframework.boot:spring-boot-starter-security'
  implementation 'org.springframework.security:spring-security-test'
  implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'

  implementation 'org.webjars:jquery:3.5.0'
  implementation 'org.webjars:jquery-ui:1.12.1'
  implementation 'org.webjars.bower:bootstrap:4.4.0'

  compileOnly 'org.projectlombok:lombok'
  annotationProcessor 'org.projectlombok:lombok'
  providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
  testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
}


<meta id="_csrf" name="_csrf" th:content="${_csrf.token}" />
<meta id="_csrf_header" name="_csrf_header" th:content="${_csrf.headerName}" />

let token = $("meta[name='_csrf']").attr("content");
let header = $("meta[name='_csrf_header']").attr("content");

$(function() {
  $(document).ajaxSend(function(e, xhr, options) {
    xhr.setRequestHeader(header, token);
  });
});


spring:
  security:
    user:
      name: admin
      password: admin

 

 

: