Tuesday 31 January 2023

Java 17 Features

 Some of the new features and improvements introduced in Java 17 include:

  1. Sealed Classes: Sealed classes provide a way to limit the implementation of a class to a specific set of classes or interfaces, making it easier to enforce contracts and reduce the risk of errors.

  2. Records: Records provide a compact and easy-to-use syntax for declaring simple data classes that represent an immutable value.

  3. Pattern Matching for instanceof: Java 17 introduces pattern matching for the instanceof operator, making it easier to write type-safe and concise code.

  4. Improved Concurrency: Java 17 includes several improvements to the Java concurrency API, including the addition of new classes and methods to simplify the development of concurrent applications.

  5. Text Blocks: Java 17 includes text blocks, a new feature that makes it easier to work with multi-line string literals in your code.

  6. Foreign Linker API: Java 17 introduces the Foreign Linker API, which provides a way to link native code and libraries directly into a Java program, improving the performance and integration of Java applications with native code.


Here is the example of sealed class

sealed interface Shape permits Circle, Rectangle { }

final class Circle implements Shape {
    private final double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }
}

final class Rectangle implements Shape {
    private final double length;
    private final double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    public double getLength() {
        return length;
    }

    public double getWidth() {
        return width;
    }
}


In this example, Shape is a sealed interface that permits the classes Circle and Rectangle to implement it. This means that no other classes can implement the Shape interface. By using sealed classes, you can restrict the types that can implement an interface and ensure type safety in your code.

Java Records is a new feature introduced in Java 16 that provides a compact syntax for declaring classes that are purely transparent data carriers. Records are a way to define simple data classes that have a private final field for each component, a public constructor, and automatically generated accessor methods (getters), equals, hashCode and toString methods.

Here is an example of how you could define a Person record in Java:

record Person(String name, int age) { }


This record definition is equivalent to the following class definition:

class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return age == person.age && Objects.equals(name, person.name); } @Override public int hashCode() { return Objects.hash(name, age); } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }

With records, you can define data classes in a more concise and readable way, while still having the benefits of automatically generated accessor methods, equals, hashCode, and toString methods.

Pattern matching in Java is a new feature introduced in Java 16/17 that provides a more concise and type-safe way to perform type checking and extract values from objects. With pattern matching, you can use the instanceof operator in a switch expression to match objects against a specific pattern and extract values from the matched object.

Here's an example of how you could use pattern matching in a switch expression to match an object against a specific type and extract values from the matched object:


public static void printArea(Shape shape) {
    switch (shape) {
        case Circle c:
            System.out.println("The area of the circle is " + Math.PI * c.getRadius() * c.getRadius());
            break;
        case Rectangle r:
            System.out.println("The area of the rectangle is " + r.getLength() * r.getWidth());
            break;
        default:
            System.out.println("Unknown shape");
            break;
    }
}


In this example, the printArea method takes a Shape object as an argument and uses a switch expression to match the object against specific patterns. If the object is a Circle object, the radius is extracted and used to calculate the area. If the object is a Rectangle object, the length and width are extracted and used to calculate the area. If the object is not a Circle or a Rectangle, the default case is executed and an "Unknown shape" message is printed.

Pattern matching provides a more concise and type-safe way to perform type checking and extract values from objects, and can make your code more readable and maintainable.


In Java, pattern matching is performed using the instanceof operator in a switch expression. The instanceof operator is used to check the type of an object and determine whether it matches a specified pattern.

Here's an example of how you could use the instanceof operator in a switch expression to match an object against a specific type and extract values from the matched object:


public static void printArea(Object shape) { if (shape instanceof Circle) { Circle c = (Circle) shape; System.out.println("The area of the circle is " + Math.PI * c.getRadius() * c.getRadius()); } else if (shape instanceof Rectangle) { Rectangle r = (Rectangle) shape; System.out.println("The area of the rectangle is " + r.getLength() * r.getWidth()); } else { System.out.println("Unknown shape"); } }


In this example, the printArea method takes an Object object as an argument and uses the instanceof operator in an if-else statement to check the type of the object. If the object is a Circle object, the radius is extracted and used to calculate the area. If the object is a Rectangle object, the length and width are extracted and used to calculate the area. If the object is not a Circle or a Rectangle, the else case is executed and an "Unknown shape" message is printed.

The instanceof operator can be useful for performing type checking and extracting values from objects, but it can also lead to code that is verbose and harder to maintain. The introduction of pattern matching in Java 16 provides a more concise and type-safe way to perform type checking and extract values from objects.



Sunday 22 January 2023

AWS Security- GuardDuty

 GuardDuty is an intelligent threat detection service

identifies malicious activity or unauthorised activities, such as anomalous behaviour, credential exfiltration, or command and control infrastructure (C2) communication is detected.

GuardDuty provides broad security monitoring of your AWS accounts, workloads, and data to help identify threats, such as attacker reconnaissance; instance, account, bucket, or Amazon EKS cluster compromises; and malware

GuardDuty is a regional service

GuardDuty analyses CloudTrail data events for Amazon S3 logs, CloudTrail management event logs, DNS logs, Amazon EBS volume data, Kubernetes audit logs, Amazon VPC flow logs, and RDS login activity.

Able to send notifications using cloudwatch events.

produces security reports called findings.

GuardDuty does not look at historical data,

GuardDuty operates completely independent of your AWS resources and therefore should have no impact on the performance or availability of your accounts or workloads.

GuardDuty does not manage or retain your logs


Not capable of doing any resource changes, like rate-limiting protection or DDOS attack migration.

https://docs.aws.amazon.com/guardduty/latest/ug/what-is-guardduty.html.

Unauthorised infra, unusual api calls, password strengths etc,,,







AWS Security- AWS Shield

 AWS Shield: Managed DDOS Protection service

- provides protection against Distributed Denial of Service (DDoS) attacks for applications running on AWS.

Shield comes with 2 tiers: Standard and Advanced

-AWS Shield Standard is automatically enabled to all AWS customers at no additional cost

- AWS Shield Advanced is an optional paid service,  provides additional protections against more sophisticated and larger attacks for your applications running on Amazon Elastic Compute Cloud (EC2), Elastic Load Balancing (ELB), Amazon CloudFront, AWS Global Accelerator, and Route 53. DRT support during DDOS attacks

-Mitigate different type of flood attacks (layer 3 and 4) attacks such as SYN/UDP floods, reflection attacks

Protects the applications that use Amazon EC2, Elastic Load Balancing (ELB), Amazon CloudFront, AWS Global Accelerator, and Route 53

Pricing

  • Shield Standard  no additional charge.
  • Shield Advanced paid service, requires a 1-year subscription commitment and charges a monthly fee, plus a usage fee based on data transfer out from CloudFront, ELB, EC2, and AWS Global Accelerator.
https://aws.amazon.com/shield/faqs/










Saturday 21 January 2023

AWS Security- WAF

 WAF-- Web Application Firewall service

WAF protect web applications from common web exploits.

WAF allows you to create custom rules that block common web exploits like SQL injection and cross site scripting.

WAF lets you create rules to filter web traffic based on conditions that include IP addresses, HTTP headers and body, or custom URIs.

WAF can be integrated with Cloudfront, ALB, API Gateway and AWS AppSync

WAF charges based on the number of web ACL, no.of rules that you add per web ACL, and the number of web requests that you receive.

WAF provides Geo match condition, blocks requests from certain countries, allow request only from certain countries.

https://aws.amazon.com/waf/faqs/

https://aws.amazon.com/waf/features/

https://docs.aws.amazon.com/waf/latest/developerguide/what-is-aws-waf.html