-->

Welcome to our Coding with python Page!!! hier you find various code with PHP, Python, AI, Cyber, etc ... Electricity, Energy, Nuclear Power

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, 30 April 2023

How to implement Singleton Design Pattern in Java using Double Checked Locking Idiom?

How to implement Singleton Design Pattern in Java using Double Checked Locking Idiom?

To implement the Singleton Design Pattern in Java using Double Checked Locking Idiom, you can follow these steps:

  1. Declare a private static volatile instance variable of the class.
  2. Create a private constructor for the class to prevent direct instantiation.
  3. Create a public static method that returns the instance of the class.
  4. Check if the instance variable is null, and if it is, synchronize the code block and check again inside the synchronized block.
  5. If the instance variable is still null, create a new instance of the class and assign it to the instance variable.
  6. Return the instance variable.

Here's an example implementation of the Singleton Design Pattern in Java using Double Checked Locking Idiom:

"Singleton Design Pattern", "Double Checked Locking Idiom", "Java", "Software Design Patterns", "Object Oriented Programming"

java
public class Singleton { private static volatile Singleton instance; private Singleton() { // Private constructor to prevent direct instantiation } public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } }

Sunday, 3 October 2021

How to get started with the Google Translate API

Even if most of us can’t travel as we once did, the world is a more accessible place, at least online. Business people may not be attending international conferences or flying around the world for meetings so much. But in many respects, businesses are finding that they can access and develop foreign markets by localizing their websites and apps to speak the language of locals and adapt to their standards. Developers can play an integral role in the localization process.

How localization tools are driving innovation in AI-driven APIs

Developers can hop on this trend, increase the value of their software, and expand online on capabilities to their apps. Google has emerged as a leader in translation algorithms in the past decade, leveraging advances in AI-driven neural network tech. This powers Google Translate, well-known to businesses for their companies or clients. They can do so readily by adding auto-translators Google Translate, well-known to businesses and consumers, an app that has made an impact on the translation profession as we know it. We’ll explore here how developers can add Google-powered translations to their apps by leveraging the powers of the company’s Translate API. Localization tools, which drive greater accuracy in translation mechanics, is also necessary for the translator to work more efficiently.

Before diving into the software weeds, it’s worth noting alternatives to the Google Translate API route. Third-party conversion tools like Zapier and IFTTT let you link your software workflow to auto-translation modules via webhooks and web services, with a minimum of coding. Even a tech-savvy non-programmer should be able to implement these solutions. The main drawback, however, is that you are likely to settle for a translation engine inferior to the one offered by Google.

What does Google offer for translation?

Google is a pioneer in both machine language and machine learning—the two L-words representing two sides of the same coin. Language needs to be learned, and that learning is achievable by mastery of a natural language. AI-driven mastery these days is driven by neural machines or NMT in 2016, bringing a “paradigm shift” in translation tech. From that year forward, NMT has been the preferred method of translating.

Back in 2006, Google started training its translation algorithm by digesting tens of millions of words extracted from translated documents of the European Union parliament and the United Nations. Today Google confronts competition from Facebook, which is leveraging the learnings from comments and posts by its 2 billion users to translate more casual conversations, including rendering LOL and WTF in scores of languages. NMT continues to be a way to go.

Happily, the language learning process didn’t stop with bureaucratese and Emoji. Google Translate today supports over 100 languages, several dozen with voice support. You can talk in one language and get the translation vocalized in another, usually with a choice of voice. And, as we will see, machine learning has been productized so that you can effectively translate a domain-specific language of your own.

Getting started with Google Translate API

Google promotes its API as fast and dynamic, adaptable to diverse content needs. The company markets not just to professional coders but to a broader spectrum of users, including those with “limited machine learning expertise” who can quickly “create high-quality, production-ready models.”

For the latter, you can just upload translated language pairs (a structured list of words/phrases with their translations) and AutoML Translation will train a custom translation model. The workflow allows either customized-by-the-client or pre-trained (by Google) inputs. To translate an English product description into French, Korean, and Portuguese, for example, you could customize a Google AutoML model for French and rely on an off-the-shelf pre-trained model for Korean and Portuguese. Then you simply upload your English HTML file to Google Cloud Storage and send a batch request to the Translation API pointing to your AutoML and the pre-trained models. Google’s AutoML Translation will then output your HTML in three separate language files to your Cloud Storage.

Training is key, but the initial model is pre-trained to render 100+ languages. And if you have a domain-specific lexicon (medical or legal terms, for example) these require just a little more training and tweaking of the basic API, if they don’t already exist. A Glossary lets users “wrap” proprietary terminology not to be translated (like brand and product names) to ensure they stay intact during translation. There is also built-in support Media Translation API, which handles real-time, low latency streaming of audio translations. 

The process is essentially three steps: Upload a language pair. Train AutoML. Evaluate.

This translation power is not free but the pricing is fair. Typically, you’ll be using Google’s Translate API and its Media Translation API (if you need voice support). You’ll need the AutoML service only if you need to train more language pairs. 

The fee for the Translate API is $20 per million characters. The Media Translation API will set you back $0.068 to $0.084 per minute. AutoML is a bit pricier, costing $45 per hour for training a language pair, to a max of $300/pair. Pay only for what you use, as you use it. (Google is patient: it wants to get you hooked, so it throws in free processing as you get up to speed, with a full year to practice before needing to pay up.)

Setting up for Your First Translation

The RESTful Translate API is the easiest way to get started. Google offers a basic and advanced setup. You can do this with localization tools, but you can also do it manually. If you’ve set up any Google API service, you’re probably comfortable with the drill and may already have a Cloud Console Account. Assuming this is true, the next things you need to do, if you haven’t already, are: 

  • Create or select your project.
  • Enable the Cloud Translation API.
  • Create a service account.
  • Download your private key in JSON format. Keep the full path to this file for the next step. 

Go to the shell prompt on your Mac OS X, Linux, or Windows (Powershell) system and set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of your JSON service account key using the following commands. This variable only applies to the current shell session. If you open a new session, you’ll need to reset this variable, replacing [PATH] with the path of the JSON file with your key.

If you’re using Linux or macOS:

export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"

For Windows, in PowerShell:

$env:GOOGLE_APPLICATION_CREDENTIALS="[PATH]"

Or, from a command prompt: 

set GOOGLE_APPLICATION_CREDENTIALS=[PATH]

Then install and initialize Google’s Cloud SDK. Depending on which operating system you’re using, the Cloud SDK may have a dependency on a version of Python that isn’t installed on your system. So be sure to double-check the Cloud SDK documentation to ensure the appropriate version of Python is installed.

Executing Your First Translation

Make a Translation API Request with a REST call using the v2 translate method.

Use curl to make your request to the https://translation.googleapis.com/language/translate/v2 endpoint.

The command includes JSON with (1) the text to be translated (q), (2) the language to translate from (source), and (3) the language to translate to (target).

Source and target languages are identified with ISO-639-1 codes. In this example, the source language is English (en), the target is French (fr). The query format is plain “text”.

The sample curl command uses the gcloud auth application-default print-access-token command to get an authentication token.

curl -s -X POST -H "Content-Type: application/json" \
    -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
    --data "{
  'q': 'The quick brown fox jumps over the lazy dog',
  'source': 'en',
  'target': 'fr',
  'format': 'text'
}" "https://translation.googleapis.com/language/translate/v2"

The response should resemble the following:

{
  "data": {
    "translations": [
      {
        "translatedText": "Le renard brun rapide saute par-dessus le chien paresseux"
      }
    ]
  }
}

Congratulations! You’ve sent your first request to the Cloud Translation API!

Next steps in the translation process

For most apps, you can rely on one of the over 100 language pairs already trained and tested.  (If the pair you require is not available, or you need a custom translation with the AutoML training module.) The full process is as follows:

  1. Create a file containing the desired language pairs, using the CURL example above. Choose source and target languages from the list here (e.g., “en” or “fr”).
  2. Write code that reads the content of your website and makes a REST call to the Cloud Translation API (including a parameter pointing to your model and then producing a translated version of that text). 
  3. Create a new page in your content management system to contain and then display the translated text. Even better, if your CMS is programmable (either directly or by way of API), improve the code by automating this step.
  4. Configure your CMS and website to display the appropriate pages when a specific language is selected by your site’s end users.

Client libraries are currently available for seven popular programming languages – C#, Go, Java, Node.js, PHP, Python, and Ruby. Just install the library of your choice. Go to Translation Client Libraries for installation instructions.


Sunday, 26 September 2021

ZERO TRUST WITH MICROSERVICES – IT’S EASIER THAN YOU THINK!

If you think these security problems with microservices through to their logical conclusion, you will see that only a zero trust approach can provide sufficient security protection. Zero trust means that basically no (micro) service can be trusted, not even if it is located in a trusted zone.

As a rule, microservice projects consist of several individual services operated separately as deployment units and call each. This has consequences for the whole system’s security. Each individual microservice must obey certain security guidelines. But this alone is not enough; communication with the other services must also be secured. Only through both measures can the network of services achieve a security standard, which is much easier to achieve in a monolithic system. The administrative handling of certificates for Transport Layer Security (TLS) can cause considerable effort in a distributed system.

 

The high number of independent services increases the probability of a security gap. As soon as new Common Vulnerabilities and Exposures (CVEs) are published, they must quickly be patched in several services. This security risk is only eliminated when all microservices have been redeployed. A potential attacker is spoiled for choice as to which service they should try to compromise first. If their attempt at attacking the first microservice is unsuccessful, then there are still plenty of other “victims” to penetrate via the network of microservices. If the services are accessed via an unsecured connection, the hacker has even more opportunities to break into the overall system. 

Zero Trust

If you think these security problems with microservices through to their logical conclusion, you will see that only a zero trust approach can provide sufficient security protection. Zero trust means that basically no (micro) service can be trusted, not even if it is located in a trusted zone. Every request between the services must be authenticated (AuthN), authorized (AuthZ), and secured using TLS. A JSON Web Token (JWT) is often used as an authentication feature, which must be sent with each request, identifying the caller. This JWT is either used end-to-end, where it is not exchanged during the entire call chain, or a TokenExchangeService generates a new token for each individual request. Last but not least, the protection should take place on the HTTP or gRPC layer (OSI Layer 7 Application), and in the underlying network layers Transport and Network (OSI Layer 4 and 3). This satisfies the OWASP security principle of Defense in Depth.

 

Not every employee responsible for security wants to take this final step. Implementing zero trust is not trivial. On the contrary, employees often draw the (wrong) conclusion that the effort required for zero trust is too high, even if their inner security voice tells them it is the right approach. 

Zero Trust for Microservices

Some functionalities must be implemented for zero trust in microservices as a consequence of our previous considerations.

 

Each request must contain information about the caller. The easiest way to do this is to send a JWT as a Bearer type in the HTTP Authorization header. The called service validates this JWT and then performs the necessary authorization checks. If a service mesh tool is used, this check can also be performed or supplemented by a sidecar (more on this in the following sections).

 

SSL certificates are required to secure communication with TLS. They become invalid after a certain time interval and must be replaced. This cannot be done manually due to the high number of microservices. So, service mesh tools offer sufficient automation for this administrative task. This means that the amount of effort for OPs colleagues approaches zero.

 

To comply with the OWASP security principle Defense in Depth, communication connections between microservices should be authorized or prevented with firewall rules. This secures communication at the TCP/IP level. For this, Kubernetes provides the concept of network policies. More on this in the following sections.

Typical starting point

Most microservice projects running on Kubernetes start with the following situation (see Figure1: low secure deployment):

 

Figure1: low secure deployment

 

For Ingress communication, cloud operators provide suitable components. You can learn how to easily install these components through tutorials. The communication path from the Internet usually goes through a firewall and is routed to the Kubernetes cluster with a load balancer. Cloud providers often provide ready-made solutions for this, which can be easily converted to TLS.

 

Now begins the part where you have to implement further security measures out of your own responsibility. An Ingress controller in Kubernetes takes care of forwarding the request within the cluster. This is typically equipped as an SSL endpoint with a company’s own certificate. As a result, the requests are forwarded to the respective microservices without TLS after SSL termination. So, communication within the cluster is completely unsecured.

 

Some microservices (or already all of them?) have security programming that validates the received JWT and performs an authorization check with the claim values it contains. To validate the JWT, requests must be sent (sporadically) to the identity provider (IDP) that issued the JWT. This is usually done with TLS, since the IDP only offers HTTPS access.

 

The internal cost-benefit analysis shows that a certain level of security has been achieved with the available resources at a reasonable cost. The project team is aware that this setting does not correspond to a zero trust approach, but believes that the effort required for zero trust is far too high. For lack of better knowledge, they are satisfied with this (low) level of security.

Service Mesh

A much higher security level can be achieved with service mesh tools like Istio or Linkerd (and many more). These tools offer corresponding functionalities, which can automate certificate management. We will examine these features in detail using Istio as an example.

Mutual TLS

Each service that becomes part of a service mesh is assigned a sidecar, which controls and monitors incoming and outgoing communication to the service. The sidecar’s behavior is controlled by a central control component that transmits the right information and instructions to the sidecar. The pod consists of the service and the sidecar. When started, the sidecar retrieves an individual SSL certificate from the central control unit. This enables the sidecar to establish a mutual TLS (mTLS) connection. Only the request from the sidecar to the service (communication within the pod) takes place without TLS. After a predefined interval (for Istio, the default is 24 hours), the sidecar is automatically issued a new certificate from the control service. For the next 24 hours, this new certificate is used for the mTLS connection.

 

With the following Istio rule, mTLS communication becomes mandatory for the entire service mesh:

 

1
2
3
4
5
6
7
8
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
   name: default
   namespace: istio-system
spec:
   mtls:
    mode: STRICT

 

For a transitional solution where not all services have been integrated into the service mesh yet, there is a special mode: PERMISSIVE. Services within the service mesh are addressed with mTLS and services running outside the service mesh continue to be called without TLS. This is done automatically and is executed by the sidecar that initiates the call.

 

The security advantage is obvious. Communication within the service mesh takes place via mTLS and certificate management runs automatically at short intervals. The entire mTLS handling is performed transparently without any intervention in the service’s code, from the service’s point of view. Protection on the application layer of the network stack can be combined with the protection on layers 3 and 4 without issue (see the following sections).

Ingress Gateway

Istio offers Ingress Gateway, which controls the entry point into the Kubernetes cluster and into the service mesh. It can completely replace the Ingress Controller from the previous system landscape (see Figure1: low secure deployment).

 

You could also run Istio without the Ingress Gateway, but you will lose a lot of functionality. The advantage of using the Ingress Gateway is that all Istio rules apply. So, all rules for traffic routing, security, releasing, etc. are applicable. Communication from the Ingress gateway to the first service is already secured with mTLS. In the case of an alternative Ingress Controller, this request would not have SSL protection. The name Gateway (instead of Controller) was deliberately chosen by Istio, because it can be operated as an API Gateway with Ingress Gateway’s existing functionality.

 

For secure entry into the cluster, the Ingress Gateway can be configured with the appropriate company certificate, much like the procedure for an Ingress Controller. The following Istio rule defines how the Ingress Gateway works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
apiVersion: networking.istio.io/v1alpha3
 
kind: Gateway
 
metadata:
 
     name: mygateway
 
spec:
 
     selector:
 
      istio: ingressgateway
 
     servers:
 
     - port:
 
      number: 443
 
      name: https
 
      protocol: HTTPS
 
    tls:
 
      mode: SIMPLE
 
      credentialName: mytls-credential
 
    hosts:
 
    - myapp.mycompany.de

An SSL port (443) is opened for all requests to the host name myapp.mycompany.de and the appropriate SSL certificate is read from the Kubernetes Secret mytls-credential. The SSL secret is created with the following Kubernetes rule:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: v1
 
kind: Secret
 
metadata:
 
name: mytls-credential
 
type: kubernetes.io/tls
 
data:
 
tls.crt: |
 
XYZ...
 
tls.key: |
 
ABc...

The input to the cluster is also secured with TLS and further communication is done with Istio’s mTLS setting.

Network Policy

After the HTTP layer is secured with SSL, further network layers (OSI Layer 3 and 4) should now be secured. The concept of Network Policy establishes something like firewalls in Kubernetes. These policies define the network connections between pods and enforces compliance by a previously installed network plugin. Network policies without a network plugin have no effect. Kubernetes offers a wide range of plugins that can be installed in a Kubernetes cluster.

 

The best practice is to define a Deny-All rule. This disallows network communication between pods throughout the cluster. The following Deny-All-Ingress rule prohibits all incoming communication on pods in the associated namespace:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: networking.k8s.io/v1
 
kind: NetworkPolicy
 
metadata:
 
name: default-deny-ingress
 
namespace: my-namespace
 
spec:
 
podSelector: {}
 
policyTypes:
 
- Ingress

After this rule has been enabled, you can enable specific Ingress connections individually. For example, the following rule enables Ingress traffic to the pod with the label app=myapp, but only if the request comes from the Ingress gateway:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
apiVersion: networking.k8s.io/v1
 
kind: NetworkPolicy
 
metadata:
 
name: access-myapp
 
namespace: my-namespace
 
spec:
 
podSelector:
 
matchLabels:
 
app: myapp
 
ingress:
 
- from:
 
- podSelector:
 
matchLabels:
 
istio: ingressgateway

For each additional connection that is allowed, the rule must either be extended or more rules must be defined.

 

By establishing the aforementioned rules, the initial deployment (see Figure1: low secure deployment) has changed into the following (see Figure 2: medium secure deployment):

 

Figure 2: medium secure deployment

 

SSL termination is performed by the Istio Ingress Gateway. Each forwarded request is secured with mTLS. Additionally, a network policy defines the intended Ingress request for each pod. All unwanted requests are prevented by the network plugin. This is a big step towards zero trust. But we are still missing authorization checks, which limit the permissibility of calls even further.

Authentication (AuthN) and Authorization (AuthZ)

For AuthN and AuthZ, Istio provides a set of rules that can be used to granularly control which calls are authorized and which are not. These rules are obeyed by sidecars and the Ingress gateway, which means that the entire defined rule set is applied everywhere in the service mesh. The corresponding application does not notice this, since it is handled transparently by the respective sidecars.

 

Authentication is based on a JSON web token verified by Istio. For this, the necessary JWT validations are executed, and the issuing identity provider (IDP) is accessed. After successful validation, the request is effectively authenticated within the entire service mesh. This is best done in the Ingress Gateway, so that the check is performed as soon as it enters the service mesh or cluster.

 

The following rule instructs Istio to validate the received JWT against the IDP with the URL https://idp.mycompany.de/.well-known/jwks.json:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
apiVersion: security.istio.io/v1beta1
 
kind: RequestAuthentication
 
metadata:
 
name: ingress-idp
 
namespace: istio-system
 
spec:
 
selector:
 
matchLabels:
 
istio: ingressgateway
 
jwtRules:
 
- issuer: "my-issuer"
 

As you can see from the rule above, the issuers are to be defined as an array, and several different IDPs can also be specified.

 

This means that the request is considered authenticated, but no authorization checks are performed. These must be specified separately with a different rule type. As with Network Policy, there is a best practice when declaring all accesses in the service mesh as unauthorized. You can define this with the following rule:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: security.istio.io/v1beta1
 
kind: AuthorizationPolicy
 
metadata:
 
name: allow-nothing
 
namespace: istio-system
 
spec:
 
{}

As we saw earlier with Network Policy, each individual access to a precisely specified pod can be controlled in a very fine, granular way. Within the rule, the from, to, and when sections can be used to define where the request must come from, which HTTP methods and endpoints should be called, and which authentication content (claims in JWT) must be included. Access will only be allowed (action: allow) when these criteria are met: 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
apiVersion: security.istio.io/v1beta1
 
kind: AuthorizationPolicy
 
metadata:
 
name: my-app
 
namespace: my-namespace
 
spec:
 
selector:
 
matchLabels:
 
app: my-app
 
action: ALLOW
 
rules:
 
- from:
 
- source:
 
principals: ["cluster.local/ns/ns-xyz/sa/my-partner-app"]
 
- source:
 
namespaces: ["ns-abc", “ns-def”]
 
to:
 
- operation:
 
methods: ["GET"]
 
paths: ["/info*"]
 
- operation:
 
methods: ["POST"]
 
paths: ["/data"]
 
when:
 
- key: request.auth.claims[iss]
 

In addition to the possibility of authorizing a request with action: allow, you can also deny certain requests (action: deny) or integrate your own authorization checks into the service mesh with action: custom. This allows you to continue using existing authorization systems, which already exist in many companies.

Final system 

The following zero trust infrastructure results after applying all of the rules (see Figure 3: secure deployment):

 

Figure 3: secure deployment

 

Along with communication secured with TLS, authorization checks have now been added. These are evaluated in each of the sidecars. The JWT sent in the HTTP header serves as the basis for authentication.

 

To achieve this final state, the following rules are necessary (see Table 1: Overview rules):

 

Table 1: Overview rules

 

FunctionRules
TLS terminationGateway-Rule and Kubernetes-Secret
mTLSPeerAuthentication
Network SegmentationNetwork-Policy (Deny-Ingress) and one Network-Policy per pod
AuthenticationRequestAuthentication
AuthorizationAuthorizationPolicy (Allow-Noting) and one AuthorizationPolicy per pod

 

In total, there are only six basic rules and two additional rules per pod for access control. With this small number of rules, you should come to a different conclusion about the effort-benefit analysis for a zero-trust infrastructure.

Conclusion

Admittedly, the effort to establish a service mesh tool in a project is not exactly small. But in addition to all the security aspects, these tools offer much more functionality. This can be a great service when operating microservices. From a purely security-focused point of view, the cost of a service mesh tool is relatively high. But in combination with other service mesh functionalities such as traffic routing, resilience, and releasing, the cost-benefit analysis is positive. Furthermore, automated certificate management is not trivial. A self-implemented solution might not be completely error-free. When it comes to security you should not allow any mistakes.

 

It can be combined with the Network Policy and established in parallel without influencing Istio. This corresponds to a Defense in Depth approach.

 

Authorization checks can be finely controlled and leave nothing to be desired. However, caution is advised, since the large amount of options can lead to complex, difficult-to-understand situations. Always consider Keep it Simple, Stupid (KISS).

 

In order to establish a Defense in Depth approach for authorization checks, you should perform an authorization check in your respective applications.

 

When it comes to auditing, currently Istio only supports Stackdriver. A larger selection of audit systems would be preferable, but this can still come in the future.

 

Overall, with a few Kubernetes or Istio rules, a zero trust infrastructure can be established that will likely stand up to any security audit.

Rank

seo