public OptionalInt resolveSystemUID()

in emr-user-role-mapper-application/src/main/java/com/amazon/aws/emr/common/system/user/LinuxUserIdService.java [79:116]


    public OptionalInt resolveSystemUID(String localAddr, int localPort,
                                        String remoteAddr, int remotePort,
                                        boolean isNativeIMDSApi) {

        if (!isLocalhost(localAddr)) {
            log.debug("Local address is not localhost on the HTTP socket!");
            return OptionalInt.empty();
        }

        OptionalInt uid;
        try (BufferedReader br = new BufferedReader(new FileReader(ipV4Path))) {
            String line;
            while ((line = br.readLine()) != null) {
                uid = getUID(line, localPort, remotePort, remoteAddr, isNativeIMDSApi);
                if (uid.isPresent()) {
                    return uid;
                }
            }
        } catch (IOException e) {
            log.error("Exception reading {} file. ", ipV4Path, e);
            // May be this succeeds with TCP6 socket!
        }

        try (BufferedReader br = new BufferedReader(new FileReader(ipV6Path))) {
            String line;
            while ((line = br.readLine()) != null) {
                uid = getUID(line, localPort, remotePort, remoteAddr, isNativeIMDSApi);
                if (uid.isPresent()) {
                    return uid;
                }
            }
        } catch (IOException e) {
            log.error("Exception reading {} file. ", ipV6Path, e);
            // TODO: re-throw this
        }

        return OptionalInt.empty();
    }