Skip to content

HADOOP-12345: Compute the correct credential length #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.hadoop.oncrpc.security;

import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.oncrpc.XDR;
Expand Down Expand Up @@ -66,4 +67,9 @@ public static void writeFlavorAndCredentials(Credentials cred, XDR xdr) {
protected Credentials(AuthFlavor flavor) {
super(flavor);
}

@VisibleForTesting
int getCredentialLength() {
return mCredentialsLength;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.InetAddress;
import java.net.UnknownHostException;

import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.io.Charsets;
import org.apache.hadoop.oncrpc.XDR;

Expand Down Expand Up @@ -63,6 +64,11 @@ public int[] getAuxGIDs() {
return mAuxGIDs;
}

@VisibleForTesting
int getStamp() {
return mStamp;
}

public void setGID(int gid) {
this.mGID = gid;
}
Expand All @@ -75,6 +81,11 @@ public void setStamp(int stamp) {
this.mStamp = stamp;
}

@VisibleForTesting
void setHostName(String hostname) {
this.mHostName = hostname;
}

@Override
public void read(XDR xdr) {
mCredentialsLength = xdr.readInt();
Expand All @@ -93,8 +104,14 @@ public void read(XDR xdr) {

@Override
public void write(XDR xdr) {
int padding = 0;
// Ensure there are padding bytes if hostname is not a multiple of 4.
padding = 4 - (mHostName.getBytes(Charsets.UTF_8).length % 4);
// padding bytes is zero if hostname is already a multiple of 4.
padding = padding % 4;
// mStamp + mHostName.length + mHostName + mUID + mGID + mAuxGIDs.count
mCredentialsLength = 20 + mHostName.getBytes(Charsets.UTF_8).length;
mCredentialsLength = mCredentialsLength + padding;
// mAuxGIDs
if (mAuxGIDs != null && mAuxGIDs.length > 0) {
mCredentialsLength += mAuxGIDs.length * 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void testReadWrite() {
CredentialsSys credential = new CredentialsSys();
credential.setUID(0);
credential.setGID(1);
credential.setStamp(1234);

XDR xdr = new XDR();
credential.write(xdr);
Expand All @@ -42,5 +43,46 @@ public void testReadWrite() {

assertEquals(0, newCredential.getUID());
assertEquals(1, newCredential.getGID());
assertEquals(1234, newCredential.getStamp());
}

@Test
public void testHostNameNotMultipleOf4() {
CredentialsSys credential = new CredentialsSys();
credential.setUID(0);
credential.setGID(1);
credential.setStamp(1234);
credential.setHostName("hadoop-nfs");

XDR xdr = new XDR();
credential.write(xdr);

CredentialsSys newCredential = new CredentialsSys();
newCredential.read(xdr.asReadOnlyWrap());

assertEquals(0, newCredential.getUID());
assertEquals(1, newCredential.getGID());
assertEquals(1234, newCredential.getStamp());
assertEquals(32, newCredential.getCredentialLength());
}

@Test
public void testHostNameMultipleOf4() {
CredentialsSys credential = new CredentialsSys();
credential.setUID(0);
credential.setGID(1);
credential.setStamp(1234);
credential.setHostName("apachehadoop");

XDR xdr = new XDR();
credential.write(xdr);

CredentialsSys newCredential = new CredentialsSys();
newCredential.read(xdr.asReadOnlyWrap());

assertEquals(0, newCredential.getUID());
assertEquals(1, newCredential.getGID());
assertEquals(1234, newCredential.getStamp());
assertEquals(32, newCredential.getCredentialLength());
}
}